How to: Display Ads From an XML File Using the AdRotator Web Server Control

JavaScript Editor jscript editor     Web designer 



Main Page

The AdRotator Web server control reads advertisement (ad) information from a data source, which one or more ad records. You can store ad information in an XML file and then bind the AdRotator control to the file.

All attributes of the AdRotator control are optional. The following attributes can be included in the XML files:

To create an ad list as an XML file

  1. Create a new XML file in your Web site's App_Data folder. For extra security, give the file a file name extension other than .xml, such as .ads.

    NoteNote

    It is recommended that you put the ad file in the App_Data folder because ASP.NET prevents files from that folder from being served to browsers.

  2. Add the following XML elements to the file:

    В CopyCode imageCopy Code
    <?xml version="1.0" encoding="utf-8" ?>
    <Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">
    </Advertisements>
  3. Create an Ad element inside the Advertisements element for each ad that you want to include in the ad list, and then save the file.

    A sample file might look like the following:

    В CopyCode imageCopy Code
    <?xml version="1.0" encoding="utf-8" ?>
    <Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">
      <Ad>
        <ImageUrl>~/images/Contoso_ad.gif</ImageUrl>
        <NavigateUrl>http://www.contoso-ltd.com</NavigateUrl>
        <AlternateText>Ad for Contoso, Ltd. Web site</AlternateText>
        <Impressions>100</Impressions>
      </Ad>
      <Ad>
        <ImageUrl>~/images/Aspnet_ad.gif</ImageUrl>
        <NavigateUrl>http://www.asp.net</NavigateUrl>
        <AlternateText>Ad for ASP.NET Web site</AlternateText>
        <Impressions>50</Impressions>
      </Ad>
    </Advertisements>

To display ads from the database

  1. Place an AdRotator control on the ASP.NET Web page at the location where you want the ads to display.

  2. Set the AdRotator control's AdvertisementFile property to the path of the XML file that you created in the previous procedure.

    NoteNote

    It is recommended that you use a relative URL to make the application more portable. Use syntax such as ~\App_Data\filename.ad. For information about file paths, see ASP.NET Web Site Paths.

    When the page runs, the AdRotator control reads the XML file for the ads and selects one to display.

See Also



JavaScript Editor jscript editor     Web designer