System.Web.UI.WebControls.AdRotator Class

Displays an advertisement banner on a Web page.

See Also: AdRotator Members


[System.Web.UI.ToolboxData("<{0}:AdRotator runat="server"></{0}:AdRotator>")]
[System.ComponentModel.Designer("System.Web.UI.Design.WebControls.AdRotatorDesigner, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
public class AdRotator : DataBoundControl


In this topic:


Use the System.Web.UI.WebControls.AdRotator control to display a randomly selected advertisement banner on the Web page. The displayed advertisement changes whenever the page refreshes.

Advertisement information is stored in a separate XML file. The XML file allows you to maintain a list of advertisements and their associated attributes. Attributes include the path to an image to display, the URL to link to when the control is clicked, the alternate text to display when the image is not available, a keyword, and the frequency of the advertisement. Information in this file is not validated by the System.Web.UI.WebControls.AdRotator control. To prevent ads from executing malicious scripts, you should always check the data before releasing it, or accept ad information only from trusted sources.

To secure your advertisement file from unauthorized Internet access, do one or more of the following:

As an alternative to an XML file as the source of data, it is possible to provide advertisement information through a callback event. This event can also be used in combination with the XML file to extend the behavior of the System.Web.UI.WebControls.AdRotator control, such as redirecting to another page. See the AdRotator.AdvertisementFile property for additional information on the file format.


When page caching is enabled, an System.Web.UI.WebControls.AdRotator control is not cached. A new advertisement is selected whenever the Web page refreshes. A new advertisement is not selected, however, if you provide an event handler for the AdRotator.AdCreated event.

XML Advertisement File Format

The System.Web.UI.WebControls.AdRotator control uses a separate XML advertisement file to store the advertisement information, such as the location of the image to display and the URL of the page to link to. The AdRotator.AdvertisementFile property of the System.Web.UI.WebControls.AdRotator control specifies the path to this file.

When creating the advertisement file, opening and closing <Advertisements> tags mark the beginning and the end of the file, respectively. Opening and closing <Ad> tags delimit each advertisement. All advertisements are nested between the opening and closing <Advertisements> tags. If the file contains multiple <Advertisements> tags, only the first set of <Advertisements> tags in the file will be parsed by the System.Web.UI.WebControls.AdRotator control. All other <Advertisements> tags will be ignored.

The data elements for each advertisement are nested between the opening and closing <Ad> tags. Although certain data elements are predefined (such as AdCreatedEventArgs.ImageUrl and AdCreatedEventArgs.NavigateUrl), you can place custom elements between the <Ad> tags. These elements will be read by the System.Web.UI.WebControls.AdRotator control when it parses the file. The information is then passed to the AdRotator.AdCreated event in the AdCreatedEventArgs.AdProperties dictionary property.

The following table lists the data elements that are predefined for the XML advertisement file.


The absolute or relative URL to an image file (optional).


The URL of a page to link to if the user clicks the ad (optional).

If this element is not set, the HRef property is not rendered on the anchor tag.

The height of the image, in pixels (optional).


The width of the image, in pixels (optional).


The text display in place of the image when the image specified by the AdCreatedEventArgs.ImageUrl property is not available (optional).

In some browsers, this text also appears as a ToolTip for the advertisement.


A category for the advertisement (for example, "computers") that you can filter by (optional).


A number that indicates the importance of the ad in the schedule of rotation relative to the other ads in the file (optional).

The larger the number, the more often the ad is displayed. The total of all <Impressions> values in the XML file cannot exceed 2,047,999,999. If it does, the System.Web.UI.WebControls.AdRotator control throws a run-time exception.

The following example shows the format for the XML advertisement file.


      URL of image to display for Advertisement #1
      URL of page to link to for Advertisement #1
      Text to show as a ToolTip for Advertisement #1
      Keyword used to filter for Advertisement #1
       Relative importance of Advertisement #1
      Custom Data about Advertisement #1

Declarative Syntax


    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"


Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0,