Manage Ads with AdRotator Control in ASP.NET 2.0

0 comments

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'sAdvertisementFile 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 ImageUrlNavigateUrl,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" />

READ MORE >>

0 comments: