Click here to Skip to main content
12,945,401 members (82,434 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 6 Sep 2005

Alternative to Microsfot Ad Rotator

, 6 Sep 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A class that returns the ad details from an XML file.

Sample Image - AlternativeToMsAdRotator_img.jpg


This is a simple ad rotator class that reads an XML file and returns the details of the ad. Microsoft Ad Rotator control is good, but it is not very flexible to use. I wanted a way to be able to have access to the URL, image, or text of my ad at certain times which Ad Rotator can't do. Also, the MS Ad Rotator stretches the image, which doesn't look really good all the time.

What is required?

The XML file which has the details of the ads, an Image control, a Label, and a Hyperlink control.

Using the Code

namespace RahmanAdRotator
    public class AdRotator 
        private string url;
        private string img;
        private string text;
        private string xfile;
        private int thisAd;

        public AdRotator(string xmlFile,int frequency)

        //construction that gets the xml file from the user
        public AdRotator(string xmlFile)

        public string getURL()
            return url;

        public string getImage()
            return img;

        public string getText()
            return text;
        //this sub generate a random no as the random Ad
        private void generateRandom()
            int ctr = totalRecords();
            Random rnd = new Random();
            thisAd = rnd.Next(1,ctr+1);
        //this sub counts total number of records from the xml file.
        private int totalRecords()
            DataSet ds = new DataSet();
            int ctr=ds.Tables[0].Rows.Count;
            return ctr;
        //this sub reads the xml file and loops through the records 
        //and matchs the random no with the record. If matched then return it.
        private void readXML()
            DataSet ds = new DataSet();


            int ctr=1;
            //These Column names are standard Microsoft column 
            //names for ad rotator control eg ImageURL    
            foreach(DataRow row in ds.Tables[0].Rows)
                if (ctr==thisAd)
                    img = row["ImageURL"].ToString();
                    url = row["NavigateUrl"].ToString();
                    text = row["AlternateText"].ToString();
                    ctr = ctr+1;

Testing the Code

private void Page_Load(object sender, System.EventArgs e)
    //Set your own path!
    string imgPath = @"c:\inetpub\wwwroot\MobileShop\MobileImages\Nokia\";
    //Create an instance of the Adrotator and pass the path of the xml file
    AdRotator ad = new AdRotator(@"c:\inetpub\wwwroot\MobileShop\Adverts.xml");
    //use the methods to get the details of the add
    Image1.ImageUrl = imgPath + ad.getImage();
    Label1.Text = ad.getText();


I ignored the frequency of the ad as Microsoft has it in the Ad Rotator control and it can be added. There could be a lot more options that could be added! It is just an example of doing it!


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


About the Author

Rahman Mahmoodi
Software Developer
Australia Australia
Rahman is a very experienced software developer with 10+ years of experience in different programming languages. Has experience in both Web Application Development and Desktop Line of Business Application development.

At the moment his area of interest are .Net both C# and VB.Net, Client side UI frameworks like AngularJs, Bootstrap, etc. Application Architecture, Dependency Injection, Use case Driven Development, Test Driven Development, MOQ etc.

He has Bachelor of Computing with Distinction Grade from University of Western Sydney.

You may also be interested in...

Comments and Discussions

QuestionHow to AdRotator + Flash + OnClickEvent Pin
Member #25899578-Feb-07 6:04
memberMember #25899578-Feb-07 6:04 
Generalkewl dude Pin
trondborg123412344-Oct-05 19:40
membertrondborg123412344-Oct-05 19:40 

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
Web02 | 2.8.170518.1 | Last Updated 6 Sep 2005
Article Copyright 2005 by Rahman Mahmoodi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid