Click here to Skip to main content
Click here to Skip to main content
Go to top

Alternative to Microsfot Ad Rotator

, 6 Sep 2005
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

Introduction

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)
        {
            xfile=xmlFile;
            generateRandom();
            readXML();
        }

        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();
            ds.ReadXml(xfile);
            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();

            ds.ReadXml(xfile);

            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();
                    break;
                }
                else
                {
                    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();
}

Improvements

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!

License

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

Share

About the Author

Rahman Mahmoodi
Software Developer
Australia Australia
Rahman has worked in different areas of Software System Development Life Cycle. He started working as Tester and then moved to application programming and web application programming.
 
At the moment his area of interest are .Net including ASP.Net/2, VB.net, C# and object oriented design using UML.
 
He has Bachelor of Computing with Distinction Grade.

Comments and Discussions

 
QuestionHow to AdRotator + Flash + OnClickEvent PinmemberMember #25899578-Feb-07 6:04 
Generalkewl dude Pinmembertrondborg123412344-Oct-05 19:40 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Sep 2005
Article Copyright 2005 by Rahman Mahmoodi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid