AdRotator Class

Provides a server control to display a randomly selected advertisement on a mobile page.

Namespace: System.Web.UI.MobileControls
Assembly: System.Web.Mobile (in

public class AdRotator : MobileControl
public class AdRotator extends MobileControl
public class AdRotator extends MobileControl

The AdRotator class uses the same configuration file as the Web Forms AdRotator class. The System.Web.UI.MobileControls.AdRotator mobile control exposes many of the same properties and events as the System.Web.UI.WebControls.AdRotator control, plus it adds mobile capability. The displayed advertisement changes whenever the page is refreshed.

The AdvertisementFile property specifies the location of an XML file that contains advertisement information. This file must reside within the application domain. For security purposes, XML files should be in the App_Data folder, which is configured to not allow users direct access to XML files. If the AdvertisementFile property is empty, the AdRotator control generates a single break tag as a placeholder. This is useful when you do not want to display an advertisement.

The following code example consists of two parts: an ASP.NET mobile Web Forms page in an .aspx file and an XML file. The .aspx file uses a file named ads.xml to rotate through various advertisements based on the KeywordFilter property. If you provide the images for the example, they will be displayed; otherwise, the AdRotator control displays the value of the AlternateText property for the advertisements. When the user refreshes the mobile Web Forms page, the page displays the next randomly selected advertisement, based on the KeywordFilter.

The example also shows how to use a <Choice> Element (.NET Framework Developer's Guide ) element to override the properties if the user's browser requires WML markup, and how to map the image's src and href attributes to the data in the XML file using the ImageKey and NavigateUrlKey properties, respectively.

Although the example uses a function to determine whether the browser requires WML (isWML11), you can instead use a Web.config file to define a DeviceSpecific element that the .NET Framework automatically uses to make the determination for you:

   <filter name="isWML11" compare="PreferredRenderingType" argument="wml11" />


The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

The .aspx file:

<%@ Page Language="C#" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" %>
<%@ Import Namespace="System.Web.Mobile" %>

<script runat="server">
    private void AdCreated_Event(Object sender, AdCreatedEventArgs e)
       Label2.Text = "Clicking the AdRotator control takes you to " + 

    // Determine whether the current browser is a WML brower
    public bool isWML11(MobileCapabilities caps, string optValue)
        // Determine if the browser is not a Web crawler and 
        // requires WML markup
        if (!caps.Crawler && caps.PreferredRenderingType == 
            return true;
            return false;

<html xmlns="" >
    <mobile:form id="form1" runat="server">
        <!-- The AdRotator control -->
        <mobile:AdRotator id="AdControl" runat="server"
            ImageKey="MobileImgSrc" NavigateUrlKey="TargetUrl"
            AdvertisementFile="App_Data/ads.xml" Alignment="Left" 
            KeywordFilter="Developer" OnAdCreated="AdCreated_Event">
                <Choice Filter="isWML11" NavigateUrlKey="WmlTargetUrl" 
                    ImageKey= "WmlImageSrc" />
        <!-- The instructions label -->
        <mobile:Label id="Label1" runat="server" 
            Text="Refresh the page to change the advertisement" />

        <!-- The URL info label -->
        <mobile:Label id="Label2" runat="server" />

The sample ads.xml file (which must be located in the App_Data folder):

<?xml version="1.0" encoding="utf-8" ?>

  • 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 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions