The AdRotator Control


ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 22.  Rich ASP.NET Controls

The AdRotator control allows you to display a randomly selected image, selected from a list contained within an XML file, each time your page is rendered. You supply the images and the XML file containing information about the images, and the ASP.NET page framework handles the rest. You can apply filters dynamically so that you can limit the images to subsets of your choosing, as well. In the sample page, AdRotatorControl.aspx, you can select to show images of either male or female employees (or both) randomly selected from a list of images in an XML file (see Figure 22.3).

Figure 22.3. The AdRotator control allows you to cycle through a series of images.


To use the AdRotator control, you place the control on a page and set the control's AdvertisementFile property to the location of the XML file containing the image information. In addition, you can set the KeywordFilter property of the AdRotator control at any time to filter the images based on the keyword you specify.

The sample advertisement file, Ads.xml, contains information about each of the images to be displayed, like this:

 <Advertisements> <Ad>   <ImageUrl>images/buchanan.jpg</ImageUrl>   <NavigateUrl>     ClickThrough.aspx?Name=Buchanan   </NavigateUrl>   <AlternateText>Buchanan</AlternateText>   <Impressions>80</Impressions>   <Keyword>Male</Keyword> </Ad> <Ad>   <ImageUrl>images/callahan.jpg</ImageUrl>   <NavigateUrl>     ClickThrough.aspx?Name=Callahan   </NavigateUrl>   <AlternateText>Callahan</AlternateText>   <Impressions>80</Impressions>   <Keyword>Female</Keyword> </Ad> </Advertisements> 

The Advertisements and Ad tags are required, and Table 22.1 describes each of the elements within each ad.

Table 22.1. Use These Elements Within Your Advertisements File
Attribute Description
ImageUrl The URL of the image to display.
NavigateUrl The URL of the page to navigate to when the AdRotator control is clicked.
AlternateText The text to display if the image is unavailable. On some browsers, this text is displayed as a tooltip.
Impressions A value that indicates how often an advertisement is displayed in relation to other advertisements in the XML file.
Keyword The category for the advertisement. This is used by the AdRotator control to filter the list of advertisements for a specific category.


The Impressions element is somewhat confusing. The values you enter here are relative. That is, you determine the scale for the meaning of these values. If all images have equal values for this element, they're all equally likely to appear. If you have five images, and the Impressions values are 5, 4, 3, 2, and 1, then the first image is five times more likely to appear than the last. In other words, out of 15 hits, the first image is likely to appear five times, the second four times, and so on. You would get the same behavior using values such as 100, 80, 60, 40, and 20. The magnitude of these values doesn't matter, only the relative weights.

In this example, clicking the AdRotator control takes you to ClickThrough.aspx, which displays the information passed to the page, using the Request.QueryString method (see the NavigateURL elements in the sample XML):

 Private Sub Page_Load( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles MyBase.Load   If Not Page.IsPostBack Then     If Not IsNothing(Request.QueryString("Name")) Then       lblInfo.Text = "You clicked on " & _        Request.QueryString("Name")     End If   End If End Sub 

Filtering the AdRotator Control

If you supply Keyword elements in the XML advertisements file, you can filter the images displayed in the AdRotator control. Set the KeywordFilter property of the control and only see images whose keyword matches the keyword you've specified.

In the sample advertisements file, all the images contain either "Male" or "Female" as their keyword values. The sample page allows you to specify whether you want male or female images, or both. (Specifying "Both" on the sample page sets the KeywordFilter property to an empty string, effectively allowing all images.)

 Private Sub rblFilter_SelectedIndexChanged( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles rblFilter.SelectedIndexChanged   Dim strFilter As String   strFilter = rblFilter.SelectedItem.Text   If strFilter = "Both" Then     strFilter = String.Empty   End If   adEmp.KeywordFilter = strFilter End Sub 


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234 © 2008-2017.
    If you may any questions please contact us: