Introduction to the AdRotator Control

You can use the ASP.NET mobile Web AdRotator control, which is based on the AdRotator control, to randomly select and display advertisement banners. The AdRotator control automates advertisement selection, changing the displayed advertisement each time the page is refreshed. Advertisements can be weighted to control the priority level of banners, making it possible to display certain advertisements more often than others. Your application can also use custom logic to select advertisements.

To generate advertisements on a mobile Web page, you must supply the image files of the advertisements. The advertisement images can be associated with destination URLs that users will browse to when they click the advertisement's link.

An AdRotator control must be placed within a Form or Panel control, or inside a control's template on a mobile Web page. You can also drag it onto a mobile user control page. The AdRotator control requires an XML file that contains the URLs of the images. The file can also specify navigational links for each advertisement. Set the AdRotator control's AdvertisementFile property to associate the control with the XML file. For a sample XML advertisement file, see the AdRotator overview.


In Visual Studio, the advertisement images that you specify are not displayed in Design view.

Your application can target advertisements to your users. For example, people using online shopping to buy bicycles might also want to see advertisements for bicycle safety helmets. The AdRotator control's KeywordFilter property helps your application select the advertisements that are most appropriate for each user.

In addition, you can use the ImageKey property to increase the flexibility of your application's advertisement presentation. The ImageKey property contains the name of an XML element in the advertisement file, which in turn specifies the URL of the advertisement's image. By default, the name contained in the ImageKey property is "ImageUrl". As a result, the AdRotator control uses the XML tag <ImageUrl> to specify the URL of an advertisement's banner.

Applying device filters to an AdRotator control enables your application to selectively override the value of the ImageKey property based on the display capabilities of the output device. For example, the XML advertisement file can use the <ImageUrl> element as the default image source. The file can also contain <WBMPImageUrl> elements for devices that use the wireless bitmap image (.wbmp) format and <GIFImageUrl> elements for devices that support .gif images. When the applied device filters detect that the application is being viewed on a device that supports the .wbmp format, the application can use the value "WBMPImageUrl" for the value of the ImageKey property. This causes the AdRotator control to use the URL specified in the <WBMPImageUrl> element as the URL for the ad image. If the applied device filters detect that the application is running on a device that supports images in .gif format, the application uses "GIFImageUrl" as the value of the ImageKey property. As a result, the AdRotator control displays the bitmap image whose URL is specified by the <GIFImageUrl> element.

In addition, overriding property values enables your application to select alternate XML advertisement files for particular devices. This is done by setting the value of the AdvertisementFile property based on the evaluation of hardware capabilities performed by device filters.

Each time the page loads, the AdRotator control raises an AdCreated event, which your application can use to customize advertisement presentation.

For example, suppose that you want to associate starting and ending dates with the advertisements that your application presents. One technique is to add XML elements named <StartDate> and <EndDate> to the XML advertisement file. For each advertisement, the XML advertisement file would contain elements similar to the following:

  <AlternateText>Contoso Corporation banner</AlternateText>

The event handler for the AdCreated event must have a parameter of type AdCreatedEventArgs. Through this parameter, the event handler can access all XML elements between the <Ad> and </Ad> tags. Because the <StartDate> and <EndDate> elements in this example are added inside the <Ad> and </Ad> tags, the event handler can read the contents of these tags, as shown in the following C# program fragment.

private void AdCreatedEventHandler(Object sender, AdCreatedEventArgs e)
    string startDate = (string) e.AdProperties["StartDate"];
    string endDate = (string) e.AdProperties["EndDate"];
    // Additional code to process the dates goes here.

After the event handler obtains the starting and ending dates for the advertisement, it can compare them to the current date and verify whether it should show the ad's banner. If not, the handler can override the information in the <ImageUrl> and <NavigateUrl> elements in the XML file.

Because you can invent any XML element you need, your application can apply this technique to any attribute that you want to assign to the advertisements. For example, by adding <StartTime> and <EndTime> elements to each advertisement in the XML file, your application can display some advertisements in the morning and others in the afternoon.

The AdRotator control also supports data binding for several of its properties. For example, applications can bind the AdvertisementFile property to a data source that contains the names of several advertisement files. The file name in the data source can change while the application runs. Early in the day, the data source can supply the name an XML file that contains advertisements the application will display in the morning. Later in the day, the data source can supply the name of a file that contains advertisements to display during the afternoon.