Manage Ads with AdRotator Control in ASP.NET 2.0


The AdRotator control available in ASP.NET 2.0 allows you to manage and run ads on a website. The ads can be image files (gif, jpg etc) only. The AdRotator control selects and displays an ad randomly each time a page is refreshed. The selection of the ad is done based on the priority you define in the ad settings file.

 

Note: The AdRotator control displays images (gif or jpg etc) only and does not provide any functionality to collect ad statistics.

 

The simplest way to use an AdRotator control is to create an XML file with ad details in it and set it's AdvertisementFile property to the ads file as following:

 

<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/Ads.xml" />

 

See The Ads File section below on how to create an Ads file.

 

The Ads File

 

The data can come from a database, XML file, or custom code. I add an XML file called Ads.xml (See Listing 1) to the project and contents of this file. As you can see this below XML, Advertisement tag has <Ad> tags. Each <Ad> tag represents an ad. The <Ad> tag has items ImageUrl, NavigateUrl, AlternateText, and Impressions. You can also set each ad's width and height using the Width and Height tags inside <Ad> tag.

 

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

<Advertisements xmlns="http://schemas.microsoft.com/AspNet/AdRotator-Schedule-File">

  <Ad>

    <ImageUrl>~/images/dp.gif</ImageUrl>

    <NavigateUrl>http://www.dynamicpdf.com</NavigateUrl>

    <AlternateText>Dynamic PDF - Generate PDFs in .NET</AlternateText>

    <Impressions>10</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>~/images/da.gif</ImageUrl>

    <NavigateUrl>http://www.discountasp.net</NavigateUrl>

    <AlternateText>ASP.NET Hosting</AlternateText>

    <Impressions>10</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>~/images/dundas.jpg</ImageUrl>

    <NavigateUrl>http://www.dundas.com</NavigateUrl>

    <AlternateText>Dundas Software</AlternateText>

    <Impressions>10</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>~/images/redgate.gif</ImageUrl>

    <NavigateUrl>http://www.red-gate.com</NavigateUrl>

    <AlternateText>ANTS Profiler</AlternateText>

    <Impressions>60</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>~/images/netcomm.gif</ImageUrl>

    <NavigateUrl>http://www.dotnetcommerce.com</NavigateUrl>

    <AlternateText>.NET Commerce</AlternateText>

    <Impressions>10</Impressions>

  </Ad>

</Advertisements>

 

Listing 1. Ads.xml

Here is a list and description of <Ad> tag items.

  • ImageUrl - The URL of the image to display.
  • NavigateUrl - The URL where the page will go after AdRotator image is clicked.
  • AlternateText - Text to display if the image is unavailable.
  • Keyword - Category of the ad, which can be used to filter for specific ads.
  • Impressions - Frequency of ad to be displayed. This number is used when you want some ads to be displayed more frequently than others. This number can not be more than 2,048,000,000 – 1.
  • Height - Height of the ad in pixels.
  • Width - Width of the ad in pixel. 

Using the Width and Height elements, you can display multiple ad sizes in same AdRotator control.

 

Set Other Data Sources

 

A Data Source can be any DataSource controls including SqlDataSource, ObjectDataSource, or XmlDataSource, it depends on where the data is coming from. For example, if data is coming from a SQL Server database, you will create a SqlDataSource but if data is coming from an XML file, the XmlDataSource should be used.

 

In our case, we are using XML file as data. So, we create an XmlDataSource control and set the DataFile property to the Ads.xml file.

 

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Ads.xml">

</asp:XmlDataSource>

 

The AdRotator Control

 

The <asp:AdRotator> tag is used to create an AdRotator control. It has ID, DataSourceID, Height, and Width properties. The following code sets the data source, width, and height of AdRotator.

 

<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="XmlDataSource1" Height="60px" Width="100px" />

 

Filtering Ads for Keywords

 

The AdRotator control has a property called KeywordFilter, which is used to filter ads for matching keywords. For example, if you have some ads that should be displayed on Products pages only, you can set AdRotator.KeywordFilter = "Products" in your code.

 

AdRotator1.KeywordFilter = keyword;

 

You also have to make changes to your Ad file to add a Keyword tag in the XML file. The AdRotator will display only ads that have Keyword as "Products".

 

<Ad>

    <ImageUrl>~/images/redgate.gif</ImageUrl>

    <NavigateUrl>http://www.red-gate.com</NavigateUrl>

    <AlternateText>ANTS Profiler</AlternateText>

    <Impressions>60</Impressions>

    <Keyword>Products</Keyword>

  </Ad>

 

Building a Dynamic Ad Management System

 

On C# Corner, we have a dynamic ad management system that means we can add, delete, and update, change the impressions and keywords of the ads anytime we want.

 

To do this, the better approach is to use a database rather XML file to store your ads data. Try these steps:

 

1. Create a database table with Adfile columns - ImageUrl, NavigateUrl, Impressions, Keywords, Width, and Height.

2. Create a SqlDataSource and fill data from the database

3. Set DataSource of AdRotator as this new DataSource.

 

Summary

 

This step by step tutorial shows you how to use AdRotator control to create a dynamic ad management for your website. 

 

 

 

Up Next
    Ebook Download
    View all
    Learn
    View all