AdRotator Using ASP.Net

Background

AdRotator is widely used in today's business to display advertisements in the form of graphics, images, event details and so on to provide summary details about specific things. In addition to that AdRotator is used for many other purposes depending on business needs. Now let us learn how to use AdRotator step-by-step.

The following are the few properties that are commonly used.

Advertisement File: This is used to specify the advertisement file details that are mostly in the form of XML.

AlternateText Field
: This property determines which text to be displayed when an Image is not available on AdRotator for technical reasons.

ImageUrl Field: URL of the Image that is to be displayed using the AdRotator.

Keyword Filter:
This property filters the advertisement using a specific keyword.

NavigateUrl Field: This is the URL that navigates you to a specific location after clicking the advertisement.

Now let us create the application to demonstrate the use of the AdRotator control.
Use the following procedure to create a web site:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New" - "Website..." then select "C#" - "Empty Project" (to avoid adding a master page).
  3. Provide the project a name such as "UsingAdRotator" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer then select "Add New Item" - "Default.aspx" page.
  5. Create one folder by right-clicking on Solution Explorer to store Images.
  6. Add XML file by right-clicking on Solution Explorer to store Advertisement details.
  7. Drag and Drop one AdRotator control onto the Default.aspx page. Then the page will look such as follows.

Now create a XML file to store the advertisement details that will look as in the following:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Advertisements>  
  3.   <Ad>  
  4.     <ImageUrl>~/Banners/comp.jpg</ImageUrl>  
  5.     <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>  
  6.     <AlternateText>C# Corner</AlternateText>  
  7.   </Ad>  
  8.   <Ad>  
  9.     <ImageUrl>~/Banners/Csharpcorner.jpg</ImageUrl>  
  10.     <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>  
  11.     <AlternateText>C# Corner Learn Asp.net</AlternateText>  
  12.   </Ad>  
  13.   <Ad>  
  14.     <ImageUrl>~/Banners/Csharpcorner.jpg</ImageUrl>  
  15.     <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>  
  16.     <AlternateText>C# Corner</AlternateText>  
  17.   
  18.   </Ad>  
  19. </Advertisements> 
Our final Solution Explorer will look as in the following:
 
 
We have used the Ajax Update Panel  with Timer control to auto refresh the advertisement file without an entire page refresh, so after adding all the required controls our Solution Explorer will look like as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body bgcolor="gray">  
  9.     <form id="form1" runat="server">  
  10.     <h2 style="color: #800080">  
  11.         Our Clients</h2>  
  12.     <div>  
  13.         <asp:ScriptManager ID="ScriptManager1" runat="server" />  
  14.         <asp:Timer ID="Timer1" Interval="2000" runat="server" />  
  15.         <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  16.             <Triggers>  
  17.                 <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />  
  18.             </Triggers>  
  19.             <ContentTemplate>  
  20.                 <asp:AdRotator Height="150px" Width="600px" ID="AdRotator1" AdvertisementFile="~/App_Code/ClientsXMLFile.xml"  
  21.                     runat="server" />  
  22.             </ContentTemplate>  
  23.         </asp:UpdatePanel>  
  24.     </div>  
  25.     </form>  
  26. </body>  
  27. </html> 
 Now run the application, the output will look like as follows:
 
 
 
Now wait for 2000 mili-seconds, then the second advertisement file will be automatically shown because we used the Timer control and specified a Time Interval of 2000. So after 2000 mili-seconds the next advertisement file is displayed as:

 
 
Now if a click above the advertisement is made then the file will navigate you to the C# Corner Home page website.
 
  Note
  • Download the Zip file from the attachment for the full source code of the application.
  • We can also provide the advertisement file using a Data Source.
Summary

I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.
 

Up Next
    Ebook Download
    View all
    Learn
    View all