Oct 21, 2011

AdRotator control


Example 1:

 The AdRotator control allows developers to place graphical advertisements on a Web Form and provides programmatic functionality that enables the development of custom logic to track advertisement clicks. The AdRotator randomly select banner graphics from a list that is specified in an external XML.

First of all Start Visual Studio .NET. And make a new ASP.NET Web Site using visual studio 2008.

Figure1.

Add a new XML File.
Figure2.

Put this code .XML File.

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
       <Ad>
            <ImageUrl>~/Banners/468X60_add.gif</ImageUrl>
            <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>
            <AlternateText>Advertisement</AlternateText>
            <Impressions>100</Impressions>
            <Keyword>Header</Keyword>
            <Category>Script</Category>
      </Ad>
      <Ad>
            <ImageUrl>~/Banners/468X60_Consultant.gif</ImageUrl>
            <NavigateUrl>http://www.mindcracker.com/Consultants/</NavigateUrl>
            <AlternateText>Advertisement</AlternateText>
            <Impressions>100</Impressions>
            <Keyword>Header</Keyword>
            <Category>Script</Category>
      </Ad>
      <Ad>
            <ImageUrl>~/Banners/468X60_Employer.gif</ImageUrl>
            <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>
            <AlternateText>Advertisement</AlternateText>
            <Impressions>100</Impressions>
            <Keyword>Header</Keyword>
            <Category>Script</Category>
      </Ad>
      <Ad>
            <ImageUrl>~/Banners/468X60_Jobseeker.gif</ImageUrl>
            <NavigateUrl>http://www.mindcracker.com/Jobs/</NavigateUrl>
            <AlternateText>Advertisement</AlternateText>
            <Impressions>100</Impressions>
            <Keyword>Header</Keyword>
            <Category>Script</Category>
      </Ad>
      <Ad>
            <ImageUrl>~/Banners/468X60_Recruiter.gif</ImageUrl>
            <NavigateUrl>http://www.Mindcracker.com/Registration/Register.aspx</NavigateUrl>
            <AlternateText>Advertisement</AlternateText>
            <Impressions>100</Impressions>
            <Keyword>Header</Keyword>
            <Category>Script</Category>
      </Ad>
</Advertisements>

XML file elements:
·          ImageUrl : The image that will be displayed. This can be a relative link or a fully qualified internet URL.
·          NavigateUr : 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.
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.

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


There are two ways to do bind XML file on a page. First is drag and drop a XML Data Source and  configure it. Like this:

<asp:XmlDataSource ID="XmlDataSourceAd" runat="server"
                DataFile="~/Advertise.xml">
</asp:XmlDataSource>

<asp:AdRotator ID="AdRotatorHeader" runat="server" KeywordFilter="Header"
                OnAdCreated="AdRotatorHeader_AdCreated" Target="_blank" DataSourceID="XmlDataSourceAd"/>
            <asp:Label ID="LabelHeaderScript" runat="server" Visible="False"></asp:Label>

protected void AdRotatorHeader_AdCreated(object sender, AdCreatedEventArgs e)
    {
        try
        {
            if (e.AdProperties["Category"].ToString() == "Script")
            {              
                LabelHeaderScript.Text = e.AdProperties["ImageUrl"].ToString();
                LabelHeaderScript.Visible = false;
                AdRotatorHeader.Visible = true;
            }
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            AdRotatorHeader.Visible = false;
        }
    }


And socond thing is you can call XML file direct using AdvertisementFile property, like this:

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


Output should be like this.










After refreshing page then you will see second image.



Example 2:

Abstract

.NET is the new distributed computing platform developed by Microsoft and ASP.NET is its programming model for web development. Advertisements on Web pages typically take the form of ad banners � .gif files or similar images � that when clicked redirect the user to the advertisers' Web pages. In order to generate ads on your Web page you will need the image files and accompanying URLs. The intent of this article is to get a good experience in developing using the AdRotator web server control for beginners. This application will teach you how to develop a website with an ad management system. This uses XML to change ads in the web page.

Overview of the Solution

The AdRotator web server control cycles through a series of clickable ad banners, and allows some ads to be weighted more heavily than others. Ads can either be linked to the control using an XML file with a predefined schema or by creating your own custom logic. Use the AdRotator control to retrieve and display ads with one of two methods:
  • Create an XML file containing references to ad banners and their associated properties.
  • Write your own logic to select an ad banner in the AdCreated event.
XML
One method of storing ad banner image locations, URLs for redirection, and associated properties is to put the detailed information in an XML file. By using the XML file format, you can create and maintain a list of advertisements without having to change the other code inside your application when a change is made to an advertisement. The XML file can be created using the Ad Rotator Schedule File template in the XML designer or manually by the developer.

Controls used in this application
Description: http://www.codeproject.com/images/minus.gif 
protected System.Web.UI.WebControls.AdRotator AdRotator;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;


Namespaces used in this application:
Description: http://www.codeproject.com/images/minus.gif 
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI.HtmlControls;


Solution with Code

In the XML file, we can filter the ads by the keywords we declared for each ad. First you add the AdRotator control to a page, and then link it to the ads you wish to display. Link ads to the control either by using a separate XML file containing the ad information or by linking the ads to the control within an event handler at run time.
Adding the AdRotator web server control to your web application: first, select the AdRotator and drag and drop the control to your web form. Map the XML file which contains the details about each and every ad.

Description: http://www.codeproject.com/images/minus.gif 
// XML CODE THAT AS THE DETAILS ABOUT THE ADS
<Advertisements>
    <Ad>
        <ImageUrl>D:\Viv_B-Practice\AdRotator_VT\www.asp.net.gif</ImageUrl>
        <NavigateUrl>http://www.asp.net</NavigateUrl>
        <AlternateText>ASP.NET Logo</AlternateText>
        <Keyword>A</Keyword>
        <Impressions>Technology</Impressions>
        <Caption>This is the caption for Ad#1</Caption>
    </Ad>

    <Ad>
        <ImageUrl>D:\Viv_B-Practice\AdRotator_VT\www.sulekha.com.gif</ImageUrl>
        <NavigateUrl>http://www.sulekha.net</NavigateUrl>
        <AlternateText>www.Sulekha.net</AlternateText>
        <Keyword>S</Keyword>
        <Impressions>Web Site</Impressions>
        <Caption>This is the caption for Ad#2</Caption>
    </Ad>

    <Ad>
        <ImageUrl>D:\Viv_B-Practice\AdRotator_VT\FlashFile.swf</ImageUrl>
        <NavigateUrl>AdRotator.aspx?ad=Widgets
               &target=http://msdn.microsoft.com/widgets/</NavigateUrl>
        <AlternateText>www.neostream.net</AlternateText>
        <Keyword>S</Keyword>
        <Impressions>Flash Site</Impressions>
        <Caption>This is the caption for Ad#2</Caption>
    </Ad>
</Advertisements>


The above shown is the XML code. The above XML template contains the details about each ad that is going to be placed in the web application. You can create an ad list for the AdRotator control in the XML designer using the Ad Rotator Schedule File as the target schema.

The AdRotator Properties
  • ImageUrl - The URL of the image to display.
  • NavigateUrl - The URL of the page to navigate to when the AdRotator control is clicked.
  • AlternateText - The text to display if the image is unavailable.
  • Keyword - The category of the ad that can be used to filter for specific ads.
  • Impressions - A numeric value that indicates the likelihood of how often the ad is displayed. The total of all impression values in the XML file may not exceed 2,048,000,000 - 1. All attributes are optional.
The AdRotator Class

Basically, the actual AdRotator class only provides a limited set of properties:
Description: http://www.codeproject.com/images/minus.gif 
<asp:AdRotator id="controlName" runat="server"
    AdvertisementFile="ads.xml" Target="_self">
</asp:AdRotator>


Here we can see how a placeholder control creates the ad rotator control at runtime dynamically and how it works. The PlaceHolder control enables you to place an empty container control in the page and then dynamically add child elements to it at run time.

Description: http://www.codeproject.com/images/minus.gif 
// Create an AdRotator control.

AdRotator rotator = new AdRotator();

// Set the control's properties.

rotator.AdvertisementFile = "AdRotatorFiles.xml";

// Add the control to the Controls collection of a

// PlaceHolder control. 

PlaceHolder1.Controls.Add(rotator);


The PlaceHolder web server control enables you to place an empty container control within the page and then dynamically add, remove, or loop through child elements at run time. The control only renders its child elements; it has no HTML-based output of its own. As an example, you might want to have a variable number of buttons appear on a web page, depending on options selected by users. That way, users are not confronted with potentially confusing choices that are either unavailable or not relevant to their individual needs.

0 comments:

Post a Comment

Your comments:

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More