Click here to Skip to main content
15,867,488 members
Articles / Web Development / ASP.NET
Article

AdRotator Using ASP.NET Code Behind

Rate me:
Please Sign up or sign in to vote.
3.65/5 (39 votes)
2 May 2005CPOL4 min read 440.8K   11.8K   64   42
This article will teach you how to develop a website with ad management system.

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

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

Namespaces used in this application:

C#
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
// 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.NET
<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.

C#
// 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)


Written By
Chief Technology Officer at Zealots
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSolution for IMAGE not working problem.... Pin
Adhiraj Jaryal10-Jul-13 17:02
Adhiraj Jaryal10-Jul-13 17:02 
I've made this video after studying and Experimenting on AdRotator Server Control... Hope it may help you.. or may be you'll point out something to teach me.... ! Have a nice time.. --ADHIRAJ Smile | :)

https://www.youtube.com/watch?v=yQDKxOvAb6k
QuestionAdrotator performance Pin
Member 288901311-Dec-12 16:07
Member 288901311-Dec-12 16:07 
QuestionAdrotator performance Pin
Member 288901311-Dec-12 16:05
Member 288901311-Dec-12 16:05 
GeneralMy vote of 1 Pin
Member 164379229-Jun-12 0:37
professionalMember 164379229-Jun-12 0:37 
GeneralImages not Getting Dispalyed Pin
Raj Trivedi13-Jun-12 18:00
Raj Trivedi13-Jun-12 18:00 
QuestionAdrotator do not show images Pin
Amol Khandagale16-May-12 2:10
Amol Khandagale16-May-12 2:10 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey15-Mar-12 20:50
professionalManoj Kumar Choubey15-Mar-12 20:50 
QuestionAd Rotator - crashes page when using runat=server Pin
Barnsley717-Nov-11 3:28
Barnsley717-Nov-11 3:28 
GeneralMy vote of 5 Pin
GSKR18-Jun-11 2:12
GSKR18-Jun-11 2:12 
GeneralMy vote of 4 Pin
bsb2516-May-11 21:27
bsb2516-May-11 21:27 
GeneralMy vote of 2 Pin
Ashishmau13-Feb-11 22:25
Ashishmau13-Feb-11 22:25 
GeneralMy vote of 1 Pin
balakrishna.ch10-Feb-11 1:36
balakrishna.ch10-Feb-11 1:36 
GeneralMy vote of 4 Pin
vsanju14-Oct-10 22:08
vsanju14-Oct-10 22:08 
QuestionI have tryed your example Pin
Viswa Teja22-Sep-10 3:46
Viswa Teja22-Sep-10 3:46 
GeneralMy vote of 1 Pin
mukeshvalera4-Apr-10 22:46
mukeshvalera4-Apr-10 22:46 
GeneralSolution: Click on flash Pin
neviofr14-Mar-09 12:20
neviofr14-Mar-09 12:20 
GeneralClarification in ASP.NET Server Control Pin
Mohammad Ilyas1-Oct-07 20:25
Mohammad Ilyas1-Oct-07 20:25 
GeneralRe: Clarification in ASP.NET Server Control Pin
vivekthangaswamy2-Oct-07 21:12
professionalvivekthangaswamy2-Oct-07 21:12 
Generalneed help(swf files don,t run in adrotator) Pin
zumty20-Aug-07 21:00
zumty20-Aug-07 21:00 
QuestionASP.Net Pin
nickymcn18-Jun-07 20:53
nickymcn18-Jun-07 20:53 
GeneralUnable to play flash file Pin
kklahoti18-May-07 21:17
kklahoti18-May-07 21:17 
GeneralRe: Unable to play flash file Pin
vivekthangaswamy4-Jun-07 21:30
professionalvivekthangaswamy4-Jun-07 21:30 
Questionhow to use without refreshing AdRotator Pin
Kalpesh200731-Mar-07 0:03
Kalpesh200731-Mar-07 0:03 
GeneralUsing AdRotator in Windows Application Pin
Sameerkumar Namdeo20-Mar-07 1:03
Sameerkumar Namdeo20-Mar-07 1:03 
GeneralRe: Using AdRotator in Windows Application Pin
vivekthangaswamy20-Mar-07 19:23
professionalvivekthangaswamy20-Mar-07 19:23 

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.