How To Use the AdRotator Control in an ASP.NET Application with Visual Basic .NET (305035)
The information in this article applies to:
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
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 TASKSUMMARY
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.
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- Start Visual Studio .NET.
- Under Project Types, click Visual Basic Projects.
- Create a new ASP.NET Web Application project named AdvertWeb on your local computer.
- Rename WebForm1.aspx to Default.aspx.
- Save the project.
back to the top
Create the Advertisement Images- Create a new folder named Images in the AdvertWeb virtual root (which is located at C:\InetPub\WWWRoot\AdvertWeb by default).
- 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".
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:
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<!-- The URL for the ad image -->
<ImageUrl>images/microsoft.bmp</ImageUrl>
<!-- The URL the ad redirects the user to -->
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<!-- 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 -->
<Impressions>80</Impressions>
<!-- The topic of this ad (used for filtering) -->
<Keyword>ProductInfo</Keyword>
</Ad>
<Ad>
<ImageUrl>images/technet.bmp</ImageUrl>
<NavigateUrl>http://www.microsoft.com/technet</NavigateUrl>
<AlternateText>Support for IT Professionals</AlternateText>
<Impressions>40</Impressions>
<Keyword>Support</Keyword>
</Ad>
<Ad>
<ImageUrl>images/msdn.bmp</ImageUrl>
<NavigateUrl>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. - Save Adverts.xml
back to the top
Add an AdRotator Control to a Web Form- View the Default.aspx Web Form in Visual Studio.
- Drag an AdRotator control from the Web Forms section of the toolbox onto the Default.aspx Web Form.
- 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).
- Click AdRotator1 (the newly added AdRotator control), and then press the F4 key to view its properties.
- Set the AdvertisementFile property to Adverts.xml.
- Save Default.aspx, and build the project.
back to the top
Test the AdRotator Control- Start Microsoft Internet Explorer, and browse to http://localhost/AdvertWeb.
- Refresh the page several times to confirm that the advertisements appear.
- Click the advertisement, and verify that you are redirected to the appropriate Uniform Resource Locator (URL).
back to the top
Filter the Advertisements- View the Default.aspx Web Form in Visual Studio.
- Click AdRotator1, and view its properties.
- Set the KeywordFilter property to Support.
- Save Default.aspx, and build the project.
- View the page in Internet Explorer. Confirm that only advertisements with the keyword "Support" appear.
back to the top
Add Code to Track Advertisement Clicks- Return to Visual Studio.
- Double-click AdRotator1 on the Default.aspx Web Form to view its AdRotator1_AdCreated event procedure.
- Add the following Visual Basic code to the procedure:
'Change the NavigateUrl to a custom page that logs the Ad click.
e.NavigateUrl = "AdRedirect.aspx?Adpath=" & e.NavigateUrl
- Save Default.aspx
- Add a new Web Form named AdRedirect.aspx.
- 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")
'Log the ad click to a text file (you can use a database).
Dim AdFile As New IO.FileInfo(Server.MapPath("AdResponse.txt"))
Dim AdData As IO.StreamWriter
AdData = AdFile.AppendText
AdData.WriteLine(Now().ToString & ": Ad clicked. Redirect to " & _
strAdPath)
AdData.Close()
'Redirect the user to the ad URL.
Response.Redirect(strAdPath)
- Save AdRedirect.aspx, and build the project.
back to the top
Verify That the Click Tracking Code Works- Close Internet Explorer to clear the cache.
- Re-open Internet Explorer, and browse to http://localhost/AdvertWeb.
- Click the advertisement that appears.
- After you are redirected, view the contents of the AdvertWeb virtual root. A new text file named AdResponse.txt should have been created.
- Open AdResponse.txt. Notice that this file is used to record advertisement clicks.
back to the top
REFERENCES
For more information about using the AdRotator control, refer to the following .NET Framework Class Library Web site:
back to the top
Modification Type: | Minor | Last Reviewed: | 6/29/2004 |
---|
Keywords: | kbCtrl kbHOWTOmaster KB305035 kbAudDeveloper |
---|
|