5,693,062 members and growing! (19,107 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

AdRotator Using ASP.NET Code Behind

By vivekthangaswamy

This article will teach you how to develop a website with ad management system.
C#, XML, Windows, .NET 1.1, .NET, ASP.NET, WebForms, Visual Studio, VS.NET2003, Dev

Posted: 2 May 2005
Updated: 2 May 2005
Views: 109,070
Bookmarked: 40 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
24 votes for this Article.
Popularity: 5.00 Rating: 3.63 out of 5
6 votes, 25.0%
1
0 votes, 0.0%
2
4 votes, 16.7%
3
3 votes, 12.5%
4
11 votes, 45.8%
5

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

protected System.Web.UI.WebControls.AdRotator AdRotator;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

Namespaces used in this application:

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.

// 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:

<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.

// 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

vivekthangaswamy



Occupation: Architect
Location: India India

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 26 (Total in Forum: 26) (Refresh)FirstPrevNext
GeneralClarification in ASP.NET Server ControlmemberMohammad Ilyas21:25 1 Oct '07  
GeneralRe: Clarification in ASP.NET Server Controlmembervivekthangaswamy22:12 2 Oct '07  
Generalneed help(swf files don,t run in adrotator)memberzumty22:00 20 Aug '07  
QuestionASP.Netmembernickymcn21:53 18 Jun '07  
GeneralUnable to play flash filememberkklahoti22:17 18 May '07  
GeneralRe: Unable to play flash filemembervivekthangaswamy22:30 4 Jun '07  
Generalhow to use without refreshing AdRotatormemberKalpesh20071:03 31 Mar '07  
GeneralUsing AdRotator in Windows Applicationmemberccpptrain2:03 20 Mar '07  
GeneralRe: Using AdRotator in Windows Applicationmembervivekthangaswamy20:23 20 Mar '07  
GeneralRe: Using AdRotator in Windows Applicationmemberccpptrain19:36 21 Mar '07  
QuestionAdRotator + Flash + OnClick + RefreshmemberHM.NET7:16 8 Feb '07  
AnswerRe: AdRotator + Flash + OnClick + Refreshmembersuper_code1:23 13 Sep '07  
AnswerRe: AdRotator + Flash + OnClick + Refreshmembermukesh.mr031:49 25 Oct '07  
GeneralCan any one tell me how to rotate the adds without refreshing or postbacking the pagememberindian14323:16 24 Jan '07  
GeneralRe: Can any one tell me how to rotate the adds without refreshing or postbacking the pagemembertodd200010:22 10 May '07  
GeneralAdrotaor does not rotate ads in Firefoxmembervivek2313:37 8 Jan '07  
QuestionHow does Ads rotation work without refreshing the page?memberriddleastro23:25 2 Nov '06  
QuestionHow does Ads rotation work without refreshing the page?memberriddleastro23:24 2 Nov '06  
QuestionHow does Ads rotation work without refreshing the page?memberriddleastro23:22 2 Nov '06  
GeneralWhy is Impressions set to a text value?membermkamoski13:14 20 Jun '06  
GeneralAdvertisementFile http source [modified]membersteveedwards.com3:55 23 May '06  
QuestionRe: AdvertisementFile http sourcememberrocky_uday18:57 18 Jan '07  
GeneralSWF problemmemberEEmadzadeh1:00 2 Oct '05  
GeneralRe: SWF problemmembervivekthangaswamy22:31 4 Jun '07  
GeneralGood worksussAnonymous23:27 10 May '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2005
Editor: Smitha Vijayan
Copyright 2005 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project