AdRotator Class

Displays an advertisement banner on a Web page.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class AdRotator : DataBoundControl
public class AdRotator extends DataBoundControl
public class AdRotator extends DataBoundControl
Not applicable.

Use the 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 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:

  • Store it in the App_Data directory, which is configured to deny Internet access for files of any type.

  • Rename the file with an extension that is not .xml, and map the extension to the HttpForbiddenHandler in Web.config. For more information, see the httpHandlers Element (ASP.NET Settings Schema) section in your Machine.config file.

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 AdRotator control, such as redirecting to another page. See the AdvertisementFile property for additional information on the file format.

An Impressions attribute can be assigned to each advertisement. It controls how often an advertisement is selected relative to the other advertisements in the advertisement file.


When page caching is enabled, an 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 AdCreated event.

The following code example demonstrates how to use the AdRotator control to display an advertisement banner on a Web page.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
 <head runat="server">
    <title>AdRotator Example</title>
    <form id="form1" runat="server">
       <h3>AdRotator Example</h3>
       <asp:AdRotator id="AdRotator1" runat="server"

The following code example demonstrates the file format for an XML file that contains advertisement information for the previous example. For more information on the file format, see the AdvertisementFile member.

    <AlternateText>Microsoft Main Site</AlternateText>
    <AlternateText>Wingtip Toys</AlternateText>

  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand. Permission value: Minimal.
  • AspNetHostingPermission  for operating in a hosted environment. Demand value: InheritanceDemand. Permission value: Minimal.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions