Utilize advertisements on an ASP.NET site

Advertisements are an integral aspect of the Internet, and they come in all shapes, sizes, and locations. One of the more popular placements is the banner ad. The ASP.NET AdRotator control allows you to easily incorporate banner ads in your site with a few simple steps.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

Online advertisements come in various forms including the pop-up ads and banner ads, which often appear at the top of a Web site. Microsoft recognized the importance of ads by including the AdRotator Web control in the .NET Framework. This control makes it easy to incorporate banner ads in your Web applications.

Taking control

The AdRotator Web control allows you to display one image or a series of images as advertisements. You can drag and drop the control from the toolbox to a Web Form if you use Visual Studio .NET. In addition, the control may be inserted with the following:

<asp:AdRotator id="controlName" runat="server" 
Width="468px" Height="60px"
AdvertisementFile="ads.xml" Target="_self"
BorderStyle="Solid" BorderWidth="1px" BorderColor="Black">

The runat attribute must be set to server since this is a server-based control. Here is more information about the control's various properties:

  • AdvertisementFile: The path to the XML file containing ad information.
  • Id: A unique id assigned to the control.
  • Target: Where the ad's URL is opened. It has the following valid options: _blank, _parent, _search, _self, and _top.
  • BorderStyle: The style of the ad's border.
  • BorderColor: The color of the ad's border.
  • BorderWidth: The width of the ad's border. A value of zero indicates no border.
  • KeywordFilter: A filter to limit ads after categories.
  • OnAdCreated: The name of the function to be executed after the creation of the control before page rendering.

Given these properties, we can see that our sample control is displayed with a solid black border and clicking the ad's URL opens it in the same current window. The most important property is AdvertisementFile, because it determines what is displayed in the control and how the ads behave.

The AdvertisementFile is an XML file with the following elements:

  • ImageUrl: The optional path to the image file.
  • NavigateUrl: The optional URL to link to if the user clicks the ad.
  • AlternateText: The image's optional alternate text.
  • Keyword: The category assigned to the ad.
  • Impressions: The optional display rate in percent of the hits. This signals how often the ad is displayed. For example, two images may use values of 50 so that they are displayed evenly.

A sample file follows.

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

This file creates a banner that includes three advertisements. The first ad links to TechRepublic, the second ad links to CNET, and the third ad is a link to The first ad displays 40 percent of the time, and the other two ads each display 30 percent of the time. The XML file is great since it allows you to easily change ads without touching the site's source code.


As demonstrated, the XML file allows you to assign categories to ads. From this point, the AdRotator control may be configured to show ads according to filter criteria specified.

In our example, I could display only the last advertisement by setting the KeywordFilter property of the AdRotator control to "News" like this:

<asp:AdRotator id="controlName" runat="server" 
Width="468px" Height="60px" KeywordFilter="News"
AdvertisementFile="ads.xml" Target="_self"
BorderStyle="Solid" BorderWidth="1px" BorderColor="Black">

If the KeywordFilter property is utilized, two things may happen. First, if the ad source is an XML advertisement file, then ads with a matching keyword will be selected. Second, if the ad source is an XML advertisement file with no keyword matches in the ad file, then the control displays a blank image and a trace warning is generated.

Event handling

The AdRotator control's AdCreated event allows you to develop code to handle the selection of an ad. A reference to the control is passed with the event, so you may access its various properties. For example, the following VB.NET sample sets the NavigateUrl property accordingly when the ad is selected:

<script runat="server">
Sub handleEvent(sender As Object, e As AdCreatedEventArgs)
End Sub

Here's the C# equivalent:

voidhandleEvent(object sender, AdCreatedEventArgs e) {
e.NavigateUrl = "";

Now, the event is assigned to the control via its OnAdCreated property:


Let the world know

Inserting advertisements in your ASP.NET applications has never been easier than with theAdRotator control. Also, ASP.NET 2.0 still supports it with enhancements that include adding support for output caching.

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox