Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C#

Alternative to Microsfot Ad Rotator

Rate me:
Please Sign up or sign in to vote.
2.60/5 (3 votes)
6 Sep 2005CPOL 35K   131   9   2
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

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

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


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

Comments and Discussions

 
QuestionHow to AdRotator + Flash + OnClickEvent Pin
HM.NET8-Feb-07 6:04
HM.NET8-Feb-07 6:04 
Generalkewl dude Pin
trondborg_x4-Oct-05 19:40
trondborg_x4-Oct-05 19:40 
thanks man

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.