Click here to Skip to main content
13,151,514 members (47,900 online)
Click here to Skip to main content
Add your own
alternative version


62 bookmarked
Posted 2 May 2005

AdRotator Using ASP.NET Code Behind

, 2 May 2005
Rate this:
Please Sign up or sign in to vote.
This article will teach you how to develop a website with ad management system.


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


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.

        <AlternateText>ASP.NET Logo</AlternateText>
        <Caption>This is the caption for Ad#1</Caption> 

        <Impressions>Web Site</Impressions>
        <Caption>This is the caption for Ad#2</Caption> 

        <Impressions>Flash Site</Impressions>
        <Caption>This is the caption for Ad#2</Caption> 

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

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.  

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.


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


About the Author

Chief Technology Officer at Zealots
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralUnable to play flash file Pin
kklahoti18-May-07 21:17
memberkklahoti18-May-07 21:17 
GeneralRe: Unable to play flash file Pin
vivekthangaswamy4-Jun-07 21:30
membervivekthangaswamy4-Jun-07 21:30 
Questionhow to use without refreshing AdRotator Pin
Kalpesh200731-Mar-07 0:03
memberKalpesh200731-Mar-07 0:03 
GeneralUsing AdRotator in Windows Application Pin
ccpptrain20-Mar-07 1:03
memberccpptrain20-Mar-07 1:03 
Currently the AdRotator Control is used on Web pages.
Can this AdRotator Control be used in Windows Application, i.e say, on Dialog Box.
Or is there any other similar control that can be used in place of this AdRotator Control, but on Dialog box.
Actually I want something that can download some info about Ad from Server and then display that info on dialog box in the form of advertisement.

Thanks in advance.


GeneralRe: Using AdRotator in Windows Application Pin
vivekthangaswamy20-Mar-07 19:23
membervivekthangaswamy20-Mar-07 19:23 
GeneralRe: Using AdRotator in Windows Application Pin
ccpptrain21-Mar-07 18:36
memberccpptrain21-Mar-07 18:36 
QuestionAdRotator + Flash + OnClick + Refresh Pin
HM.NET8-Feb-07 6:16
memberHM.NET8-Feb-07 6:16 
AnswerRe: AdRotator + Flash + OnClick + Refresh Pin
super_code13-Sep-07 0:23
membersuper_code13-Sep-07 0:23 
AnswerRe: AdRotator + Flash + OnClick + Refresh Pin
mukesh.mr0325-Oct-07 0:49
membermukesh.mr0325-Oct-07 0:49 
QuestionCan any one tell me how to rotate the adds without refreshing or postbacking the page Pin
indian14324-Jan-07 22:16
memberindian14324-Jan-07 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 3 May 2005
Article Copyright 2005 by vivekthangaswamy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid