Export (0) Print
Expand All

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 System.Web.Mobile.dll)

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AdRotator : MobileControl
<mobile:AdRotator />

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:

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

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 " + 
           e.NavigateUrl;
    }

    // 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 == 
            MobileCapabilities.PreferredRenderingTypeWml11)
            return true;
        else 
            return false;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <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">
            <DeviceSpecific>
                <Choice Filter="isWML11" NavigateUrlKey="WmlTargetUrl" 
                    ImageKey= "WmlImageSrc" />
            </DeviceSpecific>
        </mobile:AdRotator>

        <!-- 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" />
    </mobile:form>
</body>
</html>

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

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <WebImgSrc>imgA1.gif</WebImgSrc>
    <MobileImgSrc>imgA2.gif</MobileImgSrc>
    <WmlImgSrc>imgA3.gif</WmlImgSrc>
    <TargetUrl>http://msdn.microsoft.com/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-MSDN.wml</WmlTargetUrl>
    <AlternateText>MSDN</AlternateText>
    <Keyword>Developer</Keyword>
    <Impressions>80</Impressions>
  </Ad>
  <Ad>
    <WebImgSrc>imgB1.gif</WebImgSrc>
    <MobileImgSrc>imgB2.gif</MobileImgSrc>
    <WmlImgSrc>imgB3.gif</WmlImgSrc>
    <TargetUrl>http://www.microsoft.com/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-Home.wml</WmlTargetUrl>
    <AlternateText>Microsoft</AlternateText>
    <Keyword>Customer</Keyword>
    <Impressions>90</Impressions>
  </Ad>
  <Ad>
    <WebImgSrc>imgC1.gif</WebImgSrc>
    <MobileImgSrc>imgC2.gif</MobileImgSrc>
    <WmlImgSrc>imgC3.gif</WmlImgSrc>
    <TargetUrl>http://www.microsoft.com/net/</TargetUrl>
    <WmlTargetUrl>http://OurServer/MS-Net.wml</WmlTargetUrl>
    <AlternateText>.NET</AlternateText>
    <Keyword>Developer</Keyword>
    <Impressions>80</Impressions>
  </Ad>
</Advertisements>

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft