latest Post

AdRotator Control in Asp.Net

AdRotator Control in Asp.Net

AdRotator is a control in ASP.Net which is concerned with advertisements; it basically displays a sequence of ad images.  This control uses an XML file to store the ad information. This external XML file is called the advertisement file.

 OR

 The AdRotator Web server control reads advertisement (ad) information from a data source, which one or more ad records. You can store ad information in an XML file and then bind the AdRotator control to the file.

 OR

The ad rotator is an ASP.NET control built specifically to create custom banner advertisements for a web site. The ad rotator can contain either one or many banner advertisements that it randomly displays one at a time when the page is loaded.

The ad rotator is linked to an XML file that contains all of the data for all of the advertisements, meaning that once the XML file is designed it is highly reusable over many different web pages.

The XML file must begin and end with an <Advertisements> tag. Inside the <Advertisements> tag there may be several <Ad> tags which defines each ad.

Properties of AdRotator Control:
 These are following major properties of Adrotator control:
AdvertisementFile:  Specifies the path to the XML file that contains ad information. The advertisement file is an XML file, which contains the information about the advertisements to be displayed.
 AlternateTextField: Specifies a data field to be used instead of the Alt text for an ad
ImageUrlField: Specifies a data field to be used instead of the ImageUrl attribute for an ad NavigateUrlField: Specifies a data field to be used instead of the NavigateUrl attribute for an ad KeywordFilter: Specifies a filter to limit ads after categories
Target: Specifies where to open the URL

Ads.xml
Put this code Ads.XML File as following:
<?xml version="1.0" encoding="utf-8" ?>
 <Advertisements>
   <Ad>
    <ImageUrl>~/Ads/Ads1.jpg</ImageUrl>
    <NavigateUrl>http://www.google.com</NavigateUrl>
     <AlternateText>Google Ad</AlternateText>
     <Keyword>Ads</Keyword>
     <Impressions>20</Impressions>
  </Ad>  
<Ad> 
   <ImageUrl>~/Ads/Ads2.jpg</ImageUrl>
    <NavigateUrl>http://www.yahoo.com</NavigateUrl>
     <AlternateText>Yahoo Ad</AlternateText>  
  <Keyword>Ads</Keyword>    
<Impressions>30</Impressions>
  </Ad>
  <Ad>  
  <ImageUrl>~/Ads/Ads3.jpg</ImageUrl>     <NavigateUrl>http://www.microsoft.com</NavigateUrl>
    <AlternateText>Microsoft Ad</AlternateText>
     <Keyword>Ads</Keyword>
     <Impressions>60</Impressions>
   </Ad>
  <Ad>
    <ImageUrl>~/Ads/Ads4.jpg</ImageUrl>
     <NavigateUrl>http://www.tcs.com</NavigateUrl>
     <AlternateText>TCS Ad</AlternateText>
    <Keyword>Ads</Keyword>
    <Impressions>40</Impressions>
   </Ad> 
 <Ad> 
   <ImageUrl>~/Ads/Ads5.jpg</ImageUrl>
    <NavigateUrl>http://www.mboxtuts.com</NavigateUrl>
     <AlternateText>Mbox Ad</AlternateText> 
   <Keyword>Ads</Keyword> 
   <Impressions>90</Impressions>
  </Ad>
</Advertisements> 

Now we need to create an aspx page with AdRotator Control and specify the Property i.e. AdvertisementFile to an XML File and Use ASP.NET AJAX Extension Controls such as UpdatePanel & Timer Control as following:

AdRotatorExample2.aspx Source View:
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdRotator.aspx.cs" Inherits="AdRotatorExample2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
     <title>::Mbox Tuts::</title>
</head>
<body>  
  <form id="form1" runat="server">  
      <asp:ScriptManager ID="ScriptManager" runat="server">         </asp:ScriptManager>         <div align="center"> 
           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                 <ContentTemplate>
                    <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/App_Data/Ads.xml" Width="100%" Height="400" OnAdCreated="AdRotator1_AdCreated" />
      <br />
               <asp:Label ID="lblTitle" runat="server" />
                </ContentTemplate> 
               <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />                 </Triggers> 
           </asp:UpdatePanel>
             <asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
         </div>
     </form>
 </body>
 </html>

 Code View:
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls; 

public partial class AdRotator : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e)
     {
    }
    protected void AdRotator1_AdCreated(object sender, AdCreatedEventArgs e)     {  
      lblTitle.Text = "<b>Ad Image Name: </b>" + System.IO.Path.GetFileName(e.AdProperties["ImageUrl"].ToString());
     }
}
Finally Save it and run the page. And you can noticed that ad images will automatically rotates after the specified interval without refreshing the whole page.

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment