Implementing the AdRotator Control in ASP.NET Web Forms

Last Updated: Oct 31, 2025
4 min read
Legacy Archive
Legacy Guidance: This article preserves historical web development content. For modern .NET 8+ best practices, visit our Tutorials section.

An AdRotator control in ASP.NET simplifies the task of rotating advertisement images in your web forms. This control displays a different image each time you refresh the page, making it perfect for managing multiple advertisements or banners. The control reads information about the images from an XML file that contains all necessary configuration details.

Understanding the Advertisement XML File

Your advertisement XML file (let's call it advt.xml) structures the data that drives your AdRotator control. Here's what a typical file looks like:

advt.xml
<Advertisements>

<Ad>
<ImageUrl>site1img1.jpg</ImageUrl>
<NavigateUrl>http://www.site1.com</NavigateUrl>
<AlternateText>Site1 Main</AlternateText>
<Impressions>50</Impressions>
<Keyword>Product1</Keyword>
</Ad>

<Ad>
<ImageUrl>site2img2.jpg</ImageUrl>
<NavigateUrl>http://www.site2.com</NavigateUrl>
<AlternateText>Site2 Main Page</AlternateText>
<Impressions>75</Impressions>
<Keyword>Product2</Keyword>
</Ad>

</Advertisements>

The root element Advertisements contains multiple Ad child elements. Each Ad element includes several properties that control how your advertisements display and behave.

XML Element Breakdown

The ImageUrl element specifies the path to your advertisement image. When users click the displayed image, they're redirected to the URL specified in NavigateUrl. The AlternateText element provides text that appears when the image doesn't load, and many browsers also display this as a tooltip.

The Impressions element uses a numeric value to determine how frequently an image displays relative to others. Higher values mean more frequent display. Keep the sum of all impressions below 2,047,999,999 to avoid runtime exceptions. The Keyword element categorizes your advertisement for filtering purposes.

Adding the AdRotator Control to Your Web Form

You can drag and drop an AdRotator control onto your web form in Visual Studio .NET. Here's the basic implementation:

Basic AdRotator Control
<asp:AdRotator 
    id="AdRotator1"
    Target="_self"
    AdvertisementFile="advt.xml"
    runat="server"/>

The AdvertisementFile attribute points to your XML file, while the Target attribute controls how the navigation URL opens. The AdRotator control uses HyperLink and Image controls behind the scenes to render the advertisement. Remember that the displayed image size is limited by the AdRotator control's dimensions, so adjust the control size during design time if you're working with larger images.

Programmatic Control with the AdCreated Event

You can modify the ImageUrl, NavigateUrl, and AlternateText values programmatically using the AdCreated event. This gives you dynamic control over how advertisements render:

AdCreated Event Handler
<script runat="server">
Sub AdCreated_Event(sender As Object, e As AdCreatedEventArgs) 
    e.NavigateUrl = "http://www.yoursite.com"
    e.ImageUrl = "custom-image.jpg"
    e.AlternateText = "Visit our site"
End Sub 
</script>

Wire up this event in your AdRotator control:

Control with Event Handler
<asp:AdRotator 
    id="AdRotator1" 
    runat="server"
    AdvertisementFile="advt.xml"
    Target="_self"
    OnAdCreated="AdCreated_Event"/>

Advanced Implementation Example

Here's a complete example showing how to filter advertisements by keyword:

Keyword Filtering Example
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        AdRotator1.KeywordFilter = "Product1"
    End If
End Sub

Sub AdCreated_Event(sender As Object, e As AdCreatedEventArgs)
    Response.Write("Displaying ad: " & e.AlternateText)
End Sub
</script>

<asp:AdRotator 
    id="AdRotator1" 
    runat="server"
    AdvertisementFile="advt.xml"
    Target="_blank"
    OnAdCreated="AdCreated_Event"/>

The KeywordFilter property lets you display only advertisements matching specific keywords, giving you fine-grained control over which ads appear in different sections of your site.

Conclusion

The AdRotator control provides an efficient way to display and rotate advertisements while giving you flexibility through the Impressions element to prioritize certain ads over others. By leveraging the AdCreated event and keyword filtering, you can create sophisticated advertisement management systems that adapt to different contexts and user interactions, all while maintaining clean, maintainable code in your ASP.NET Web Forms applications.

Quick FAQ

How do I control which advertisements appear more frequently?

Use the Impressions element in your XML file. An ad with Impressions set to 75 will display roughly 1.5 times more often than an ad with Impressions set to 50. The control calculates display frequency based on the ratio of each ad's impressions value to the total of all impressions. This weighted algorithm ensures that high-priority advertisements receive proportionally more visibility.

Can I track when users click on advertisements?

Yes, you'll handle the AdCreated event to capture information about which ad is being displayed, then log clicks by adding tracking parameters to your NavigateUrl. You can also use the onclick event handler on the rendered control to execute JavaScript tracking code before navigation occurs. This approach gives you comprehensive analytics about ad performance and user engagement.

Back to Articles