Implementing AdRotator Control in ASP.NET

This article demonstrates how to use the ASP.NET AdRotator control to display advertisements in an ASP.NET Web site. The attached source code also implements custom "click tracking" logic.
 

1. Introduction 

 
Many e-commerce websites use banner advertisements to promote products and services on behalf of their customers. Websites also need to not only display ads but also need to rotate. 
 
ASP.NET AdRotator control allows developers to place image ads on a Web Form and provides programmatic functionality that enables the development of custom logic to track advertisement clicks. The AdRotator control randomly selects banner ads from a list that is specified in an external XML. The XML also has a click URL.
 

2. ASP.NET Website Project

 
First of all, start Visual Studio and create a new ASP.NET Web Site. I'm using Visual Studio 2008. If you're using a newer version of Visual Studio, just create an ASP.NET Website using a Web Form or MVC.
 

Image1.jpg

Figure 1.
 

3. Create the Ad XML file 

 
Add a new XML File using Add New Item in Visual Studio. Right click on Visual Studio project name in Solution Explorer, Add New Item, and select XML file.
 

Image2.jpg 

Figure 2.
 
Add the following code or similar code in the XML. The root node of the XML file is Advertisement and it has multiple Ad tags. Each Ad tag has an ImageURL, NavigateUrl, AlternateText, Impressions, Keyword, and Category. 
 
Note: You need to replace these values with your own values. 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Advertisements>  
  3.        <Ad>  
  4.             <ImageUrl>~/Banners/468X60_add.gif</ImageUrl>            
                <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>  
  5.             <AlternateText>Advertisement</AlternateText>  
  6.             <Impressions>100</Impressions>  
  7.             <Keyword>Header</Keyword>  
  8.             <Category>Script</Category>  
  9.       </Ad>  
  10.       <Ad>  
  11.             <ImageUrl>~/Banners/468X60_Consultant.gif</ImageUrl>            
  12.             <NavigateUrl>http://www.mindcracker.com/Consultants/</NavigateUrl>  
  13.             <AlternateText>Advertisement</AlternateText>  
  14.             <Impressions>100</Impressions>  
  15.             <Keyword>Header</Keyword>  
  16.             <Category>Script</Category>  
  17.       </Ad>  
  18.       <Ad>  
  19.             <ImageUrl>~/Banners/468X60_Employer.gif</ImageUrl
  20.             <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>  
  21.             <AlternateText>Advertisement</AlternateText>  
  22.             <Impressions>100</Impressions>  
  23.             <Keyword>Header</Keyword>  
  24.             <Category>Script</Category>  
  25.       </Ad>  
  26.       <Ad>  
  27.             <ImageUrl>~/Banners/468X60_Jobseeker.gif</ImageUrl>  
  28.             <NavigateUrl>http://www.mindcracker.com/Jobs/</NavigateUrl>  
  29.             <AlternateText>Advertisement</AlternateText>  
  30.             <Impressions>100</Impressions>  
  31.             <Keyword>Header</Keyword>  
  32.             <Category>Script</Category>  
  33.       </Ad>  
  34.       <Ad>  
  35.             <ImageUrl>~/Banners/468X60_Recruiter.gif</ImageUrl>            
                <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>  
  36.             <AlternateText>Advertisement</AlternateText>  
  37.             <Impressions>100</Impressions>  
  38.             <Keyword>Header</Keyword>  
  39.             <Category>Script</Category>  
  40.       </Ad>  
  41. </Advertisements>  
 
XML file elements:
  • ImageUrl - The image that will be displayed. This can be a relative link or a fully qualified internet URL.
  • NavigateUrl - The link that will be followed if the user clicks the banner.
  • AlternateText - The text that will be displayed instead of the picture if it cannot be displayed. This text will also be used as a tooltip in
  • some newer browsers.
  • Impressions - A number that sets how often an advertisement will appear.
  • Keyword - A Keyword that identifies a group of advertisement.
  • Category - Used when have multiple categories. 

4. Place an AdRotator Control

 
Drag and drop and Ad Rotator control from the Toolbox to the Web Form. The code will look like this without the AdvertisementFile. Set its value to our Ad file.
  1. <asp:AdRotator ID="AdRotator1" runat="server" Target="_blank" />  
The actual AdRotator class provides a limited set of properties. You specify both the appropriate advertisement file in the AdvertisementFile property and the type of window that the link shouls follow in the Target property. You can also set the KeywordFilter property so that the banner will be chosen from entries that have a specific keyword.
 

5. Bind XML File 

 
There are two ways to bind the XML file on a page.
 
First is to drag and drop an XML Data Source from the Toolbox to the Web Form and configure it with our Ad XML file we've created earlier. The final code will be added to the page looks like this:
  1. <asp:XmlDataSource ID="XmlDataSourceAd" runat="server"  
  2. DataFile="~/Advertise.xml">  
  3. </asp:XmlDataSource>  
  4. <asp:AdRotator ID="AdRotatorHeader" runat="server" KeywordFilter="Header"  
  5. OnAdCreated="AdRotatorHeader_AdCreated" Target="_blank" DataSourceID="XmlDataSourceAd"/>  
  6. <asp:Label ID="LabelHeaderScript" runat="server" Visible="False"></asp:Label>  
  7. protected void AdRotatorHeader_AdCreated(object sender, AdCreatedEventArgs e)  
  8. {  
  9. try  
  10. {  
  11. if (e.AdProperties["Category"].ToString() == "Script")  
  12. {  
  13. LabelHeaderScript.Text = e.AdProperties["ImageUrl"].ToString();  
  14. LabelHeaderScript.Visible = false;  
  15. AdRotatorHeader.Visible = true;  
  16. }  
  17. }  
  18. catch (Exception ex)  
  19. {  
  20. string str = ex.Message;  
  21. AdRotatorHeader.Visible = false;  
  22. }  
  23. }  
Alternatively, you could just set the AdvertisementFile property of the AdRotator and the value is our Ad XML file we created earlier. 
  1. <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/Advertise.xml" Target="_blank" />  

6. Build and run

 
Now build and run your application.
 
The output should be like this. The ads are being displayed in the control.

Image3.jpg

After refreshing page then you will see a different image. The images are loaded randomly. 

Image4.jpg

 

Summary

 
In this article, we learned how to use the ASP.NET AdRotator control to display banner ads in a Website.
 
 


Similar Articles