ASP.NET AdRotator Control

ASP.NET AdRotator Control is used to display a set of images in a sequence which can be used as an Advertisment. ASP.NET AdRotator Control uses an XML file to store the information for the advertisement. This XML file must begin and end with an <Advertisement> tag. Within the <Advertisement> tags there may be many <Ad> tags which can be used to define different advertisements.

The following ASP.NET Tutorial teaches you how to use an ASP.NET AdRotator Control.

  1. Go to the Toolbox and under the Standard section, select AdRotator Control .

ASP.NET AdRotator Control
ASP.NET AdRotator Control

  1. Drag and drop it onto the Form area to declare it.
  2. The following is the code excerpt you get in the Source View.

<asp:AdRotator ID="AdRotator1" runat="server" />

  1. The following is shows an example use of the ASP.NET AdRotator Control .
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:AdRotator ID="AdRotator1" AdvertisementFile="~/ad1.xml" runat="server" OnAdCreated="change_url" target="_blank" />
        <p><a href="ad1.xml" target="_blank">View XML file</a></p>
    </div>
    </form>
</body>
</html>
  1. Add the following code to the Code behind file (Default.aspx.vb)
Protected Sub change_url(ByVal sender As Object, ByVal e As AdCreatedEventArgs)
    e.NavigateUrl = "http://www.aspnetbook.com"
End Sub

Note:

  • An event handler is created for the AdCreated event in the .aspx file.
  • This overrides the value of the NavigateURL element in the XML file.
  1. The following is the code for the Ad1.xml XML file
<Advertisements>
<Ad>
<ImageUrl>/banners/ad1.gif</ImageUrl>
<NavigateUrl>http://www.aspnetbook.com</NavigateUrl>
<AlternateText>Main Site</AlternateText>
<Impressions>10</Impressions>
<Keyword>Keywords Here</Keyword>
</Ad>
<Ad>
<ImageUrl>/banners/ad2.jpg</ImageUrl>
<NavigateUrl>http://www.aspnetbook.com/index.asp</NavigateUrl>
<AlternateText>Main Page</AlternateText>
<Impressions>10</Impressions>
<Keyword>Keywords Here</Keyword>
</Ad>
</Advertisements>

The following are the predefined elements inside the <Ad> tag.

Element

Description

<ImageUrl>

An Optional Element used to define the path of the Image file.

<NavigateUrl>

An Optional Element used to define the URL to link to if the user clicks the ad.

<AlternateText>

An Optional Element used to define an alternate text for the Image file.

<Keyword>

An Optional Element used to define a category for the Advertisement.

<Impressions>

An Optional Element used to display rates in percent of hits.

The following table shows the Properties available for the <Ad> tag.

Property

Description

.NET

AdvertisementFile

To define the path to the XML file this contains the Advertisement information.

1.0

AlternateTextField

To define the path to the data field used instead of the Alt text for an Ad.

2.0

ImageUrlField

To define the path to the data field used instead of the ImageURL attribute for an Ad

2.0

KeywordFilter

To specify a filter to limit Advertisements after categories.

1.0

NavigateUrlField

To specify the path to the data field used instead of the NavigateURL attribute for an Advertisement.

2.0

runat

To specify the control to server control i.e. must be set to server.

1.0

Target

To specify where to open the URL

1.0

  • For more information on the ASP.NET Web Control Standard Properties please click here.
  • For more information on the ASP.NET Control Standard Properties please click here.

Table of Contents