How To Use the AdRotator Control in an ASP.NET Application with Visual Basic .NET

Article translations Article translations
Article ID: 305035 - View products that this article applies to.
This article was previously published under Q305035
Expand all | Collapse all

On This Page


This article demonstrates how to use the AdRotator control to display advertisements in an ASP.NET Web site and how to implement custom "click tracking" logic. Many e-commerce sites use banner advertisements to promote products and services on behalf of customers. The AdRotator control allows developers to place graphical advertisements on a Web Form and provides programmatic functionality that enables the development of custom logic to track advertisement clicks.


This article assumes that you are familiar with the following topics:
  • ASP.NET Web application development with Visual Basic .NET
  • Extensible Markup Language (XML) syntax

Create a New ASP.NET Application

  1. Start Visual Studio .NET.
  2. Under Project Types, click Visual Basic Projects.
  3. Create a new ASP.NET Web Application project named AdvertWeb on your local computer.
  4. Rename WebForm1.aspx to Default.aspx.
  5. Save the project.

Create the Advertisement Images

  1. Create a new folder named Images in the AdvertWeb virtual root (which is located at C:\InetPub\WWWRoot\AdvertWeb by default).
  2. Open a graphics program such as Paint to create three images. Although this example uses the .bmp format, you can use most graphical formats, such as .bmp, .gif, or .jpg files.

    For this example, use the following guidelines to create three images, and save the images in the Images folder that you created in step 1:
    • Microsoft.bmp: 190 x 50 pixels blue rectangle that contains the text "Microsoft".
    • Technet.bmp: 190 x 50 pixels dark blue rectangle that contains the text "Technet".
    • Msdn.bmp: 190 x 50 pixels red rectangle that contains the text "MSDN".

Create an Advertisement File

  1. Return to the AdvertWeb project in Visual Studio.
  2. On the File menu, click Add New Item, and then click XML File to add an .xml file named Adverts.xml.
  3. Use the Visual Studio XML editor to edit Adverts.xml so that it reads as follows:
    		<!-- The URL for the ad image -->
    		<!-- The URL the ad redirects the user to -->
    		<!-- The alternate text for the image -->
    		<AlternateText>Visit Microsoft's Site</AlternateText>
    		<!-- The relative number of times this ad should appear -->
    		<!-- compared to the others -->
    		<!-- The topic of this ad (used for filtering) -->
    		<AlternateText>Support for IT Professionals</AlternateText>
    		<AlternateText>Support for developers</AlternateText>
    NOTE: Remember that XML is case-sensitive. Ensure that your document exactly matches the preceding code.
  4. Save Adverts.xml

Add an AdRotator Control to a Web Form

  1. View the Default.aspx Web Form in Visual Studio.
  2. Drag an AdRotator control from the Web Forms section of the toolbox onto the Default.aspx Web Form.
  5. Set the AdvertisementFile property to Adverts.xml.
  6. Save Default.aspx, and build the project.

Test the AdRotator Control

  1. Start Microsoft Internet Explorer, and browse to http://localhost/AdvertWeb.
  2. Refresh the page several times to confirm that the advertisements appear.

Filter the Advertisements

  1. View the Default.aspx Web Form in Visual Studio.
  2. Click AdRotator1, and view its properties.
  3. Set the KeywordFilter property to Support.
  4. Save Default.aspx, and build the project.
  5. View the page in Internet Explorer. Confirm that only advertisements with the keyword "Support" appear.

Add Code to Track Advertisement Clicks

  1. Return to Visual Studio.
  2. Double-click AdRotator1 on the Default.aspx Web Form to view its AdRotator1_AdCreated event procedure.
  3. Add the following Visual Basic code to the procedure:
    e.NavigateUrl = "AdRedirect.aspx?Adpath=" & e.NavigateUrl
  4. Save Default.aspx
  5. Add a new Web Form named AdRedirect.aspx.
  6. Add the following Visual Basic code to the Page_Load event procedure in AdRedirect.aspx:
    Dim strAdPath As String
    'Get the URL to navigate to.
    strAdPath = Request.QueryString("Adpath")
    Dim AdData As IO.StreamWriter
    AdData = AdFile.AppendText
    AdData.WriteLine(Now().ToString & ": Ad clicked. Redirect to " & _
    'Redirect the user to the ad URL.
  7. Save AdRedirect.aspx, and build the project.

Verify That the Click Tracking Code Works

  1. Close Internet Explorer to clear the cache.
  2. Re-open Internet Explorer, and browse to http://localhost/AdvertWeb.
  3. Click the advertisement that appears.
  4. After you are redirected, view the contents of the AdvertWeb virtual root. A new text file named AdResponse.txt should have been created.
  5. Open AdResponse.txt. Notice that this file is used to record advertisement clicks.


For more information about using the AdRotator control, refer to the following .NET Framework Class Library Web site:
AdRotator Class


Article ID: 305035 - Last Review: June 29, 2004 - Revision: 4.5
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
kbctrl kbhowtomaster KB305035

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from