Microsoft KB Archive/305035

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

Article ID: 305035

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q305035



For additional information about how to perform this task by using Microsoft Active Server Pages, click the article number below to view the article in the Microsoft Knowledge Base:

318285 How To Use the ASP Ad Rotator Installable Component in FrontPage 2002

IN THIS TASK
SUMMARY
 * Requirements
 * Create a New ASP.NET Application
 * Create the Advertisement Images
 * Create an Advertisement File
 * Add an AdRotator Control to a Web Form
 * Test the AdRotator Control
 * Filter the Advertisements
 * Add Code to Track Advertisement Clicks
 * Verify That the Click Tracking Code Works

REFERENCES



SUMMARY
This article demonstrates how to use the AdRotator control to display advertisements in an ASP.NET Web site and how to implement custom &quot;click tracking&quot; 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.

back to the top

Requirements
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

back to the top

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.

back to the top

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:
 * 1) * Microsoft.bmp: 190 x 50 pixels blue rectangle that contains the text &quot;Microsoft&quot;.
 * 2) * Technet.bmp: 190 x 50 pixels dark blue rectangle that contains the text &quot;Technet&quot;.
 * 3) * Msdn.bmp: 190 x 50 pixels red rectangle that contains the text &quot;MSDN&quot;.

back to the top

Create an Advertisement File
 Return to the AdvertWeb project in Visual Studio. On the File menu, click Add New Item, and then click XML File to add an .xml file named Adverts.xml.  Use the Visual Studio XML editor to edit Adverts.xml so that it reads as follows:    images/microsoft.bmp http://www.microsoft.com <AlternateText>Visit Microsoft's Site</AlternateText> <Impressions>80</Impressions> <Keyword>ProductInfo</Keyword> </Ad> <Ad> images/technet.bmp</ImageUrl> http://www.microsoft.com/technet</NavigateUrl> <AlternateText>Support for IT Professionals</AlternateText> <Impressions>40</Impressions> <Keyword>Support</Keyword> </Ad> <Ad> images/msdn.bmp</ImageUrl> http://msdn.microsoft.com</NavigateUrl> <AlternateText>Support for developers</AlternateText> <Impressions>40</Impressions> <Keyword>Support</Keyword> </Ad> </Advertisements> NOTE: Remember that XML is case-sensitive. Ensure that your document exactly matches the preceding code. </li> Save Adverts.xml</li></ol>

back to the top

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.
 * 3) Position the AdRotator control near the top center of the Web Form, and resize it so that it is the same size as the images that you created earlier. (To control the size more accurately, set the Height and Width properties).
 * 4) Click AdRotator1 (the newly added AdRotator control), and then press the F4 key to view its properties.
 * 5) Set the AdvertisementFile property to Adverts.xml.
 * 6) Save Default.aspx, and build the project.

back to the top

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.
 * 3) Click the advertisement, and verify that you are redirected to the appropriate Uniform Resource Locator (URL).

back to the top

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 &quot;Support&quot; appear.

back to the top

Add Code to Track Advertisement Clicks
<ol> Return to Visual Studio.</li> Double-click AdRotator1 on the Default.aspx Web Form to view its AdRotator1_AdCreated event procedure.</li>  Add the following Visual Basic code to the procedure: 'Change the NavigateUrl to a custom page that logs the Ad click. e.NavigateUrl = &quot;AdRedirect.aspx?Adpath=&quot; & e.NavigateUrl </li> Save Default.aspx</li> Add a new Web Form named AdRedirect.aspx.</li>  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(&quot;Adpath&quot;)

'Log the ad click to a text file (you can use a database). Dim AdFile As New IO.FileInfo(Server.MapPath(&quot;AdResponse.txt&quot;)) Dim AdData As IO.StreamWriter AdData = AdFile.AppendText AdData.WriteLine(Now.ToString & &quot;: Ad clicked. Redirect to &quot; & _   strAdPath) AdData.Close

'Redirect the user to the ad URL. Response.Redirect(strAdPath) </li> Save AdRedirect.aspx, and build the project.</li></ol>

back to the top

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.

back to the top

<div class="references_section">