How to use the AdRotator control in ASP.NET page in C#

This ASP.NET tutorial explains how to use the AdRotator control in ASP.NET page in C#.

By using the AdRotator control your ASP.NET web page randomly selects banner graphics from a list that is specified in an external XML schedule file. Before creating the control, you should define the XML schedule file, as shown in the next example:

 

<Advertisements>

<Ad>

<ImageUrl>ocom-larry-mark-cloud-banner-1641735.jpg

</ImageUrl>

<NavigateUrl> http://www.oracle.com/index.html </NavigateUrl>

<AlternateText> Oracle engineers hardware and software to work together in the cloud and in your data center. </AlternateText>

<Impressions>20</Impressions>

<Keyword> enterprise, applications, software, database, middleware, fusions, business, hardware, Oracle

</Keyword>

</Ad>

<Ad>

<ImageUrl> promo_lead_mountain_lion_peek.jpg </ImageUrl>

<NavigateUrl> http://www.apple.com/mac/</NavigateUrl>

<AlternateText> Discover the world of Mac. Check out MacBook, iMac, iLife, and more. Download QuickTime, Safari, and widgets for free.

</AlternateText>

<Impressions>20</Impressions>

<Keyword>iMac,iLife,Mac, MacBook</Keyword>

</Ad>

<!– More ads can go here. –>

</Advertisements>

 

The next table describes in details the properties you can use to configure <Ad> element:

Element

Description

ImageUrl

The image that will be displayed. This can be a relative link (a file in the current directory) or a fully qualified Internet URL.

NavigateUrl

The link that will be followed if the user clicks the banner.

AlternateText

The text that will be displayed instead of the picture if it cannot be displayed. This text will also be used as a tooltip in some newer browsers.

Impressions

A number that sets how often an advertisement will appear. This number is relative to the numbers specified for other ads. For example, a banner with the value 20 will be shown twice as often as a banner with the value 10.

Keyword

A keyword that identifies a group of advertisements. This can be used for filtering. For example, you could create ten advertisements and give half of them the keyword Microsoft and the other half the keyword Oracle. The web page can then choose to filter the possible advertisements to include only one of these groups.

 

The AdRotator class does not provide a big set of properties. You specify both the appropriate advertisement file in the AdvertisementFile property and the type of window that the link should follow in the Target property. You can also set the KeywordFilter property so that the banner will be chosen from entries that have a specific keyword:

<asp:AdRotator runat=”server” AdvertisementFile=”AdList.xml” Target=”_blank” />

Your code can react to the AdRotator.AdCreated event which raises when the page is being created and an image is randomly chosen from the file. You can use information about the image, provided by the event, to customize the rest of your page. You can use the next example to configure a HyperLink control so that it corresponds with the randomly selected advertisement in the AdRotator:

 

protected void Ads_AdCreated(Object sender, AdCreatedEventArgs e)

{

// Synchronize a Hyperlink control elsewhere on the page.

lnkBanner.NavigateUrl = e.NavigateUrl;

// Synchronize the text of the link.

lnkBanner.Text = “Click here for more information about our sponsor: “;

lnkBanner.Text += e.AlternateText;

}