When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Thursday, April 29, 1999

Using the Ad Rotator

We've all seen the sites which have nifty rotating banners. Each page you visit, you see a random advertisement at the top. If you click on the banner, you are taken to the advertiser's website. If you've ever considered adding such a system to your own site, you are in luck, because Microsoft provides a component that makes banner rotation painfully simple.

- continued -

The component from Microsoft is called the Ad Rotator, and comes standard with ASP. (This means that your web host provider more than likely has this component available for you to use!) The Ad Rotator isn't a solve-all; for example, the Ad Rotator doesn't track banner impressions and click-throughs. (However, with a little tweaking, we can keep track of some of these statistics.)

First off, you will need to create a text file which stores all of your banner information. This file is known as a Rotator Schedule File. The first four lines of the file are optional, but can be:

WIDTH banner width
HEIGHT banner height
BORDER border thickness

These parameters effect all of the banners listed in this file. So, if you set WIDTH 460, all banners will be shown at width 460 pixels. The REDIRECT parameter, if set, signifies that all banner clicks should be sent to a certain .ASP file.

The next line in the file is mandatory, and must be present even if the above four lines are not! The symbol is an asterik (*). After the asterik, for each banner in your rotation, you will need four lines. The first line is the location of the advertising image; the second line is the URL you want the visitors to be sent to once they click on the banner; the third line is the alt field text you want for the banner (this is the text that will show when the user moves his/her mouse over the banner); the fourth line is a numeric value signifying how often you want the banner to be displayed (a number signifying the probability of the banner being shown, a value between 1 and 100 (100, it's shown 100% of the time; 50, it's shown 50% of the time, and so on)).

For each banner that you want in your rotation, you simply repeat the four needed lines. Below is an example, one that uses the five global properties as well:

REDIRECT /scripts/SendOffSite.asp
Buy stuff at BannerCompany1!
Come shop at Acme.com!
There's nothing like visiting somecompany.com

This file would have three banners in its rotation, with banner1 showing up 20% of the time, banner2 showing up 30% of the time, and banner3 showing up 50% of the time. Whenever a user clicked on any of these banners, the user would be redirected to /Scripts/SendOffSite.asp. That file would be passed in the URL of homepage specified in the above file for the shown banner. The reason we want to redirect to this file first, is so that we can implement statistics on the number of click throughs. If you do not care about monitoring this information, you need not specify the REDIRECT line.

Now, let's look at the ASP needed to display a random banner, it's rather quite simple. Remember, this code needs to go in the .ASP file that you want to have display a random banner:

Dim ad
Set ad = Server.CreateObject("MSWC.AdRotator")


That's it! (Here, adrot.txt is the Rotator Schedule File. It can be named whatever you like. Furthermore, you may need to change the path in GetAdvertisement to properly point to the directory you put your Rotator Schedule File.)

The method GetAdvertisement prints out the HTML to display the banner and puts an HREF around the banner tag so that if it is clicked, the user is sent to the URL specified in your Rotator Schedule File.

If you have specified a REDIRECT file in your Rotator Schedule File, it will need to have the following ASP command in it:

Response.Redirect Request.QueryString("url")

This will send the user to the correct URL for the banner that was clicked. If you want to track click-throughs, you can do so in the redirect file (either through the user of text files or through a database). In an upcoming article, I will show you how to have an advertising system which keeps track of impressions and click throughs, and presents a nifty little report for the advertisers to see how their advertisements are doing.

I hope I've answered any questions you've had about Ad Rotator, and how to add rotating banners to your ASP site! Have a great day and Happy Programming!

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article