DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Using the AdRotator Control in ASP.NET 2.0 and VB

This tutorial will show how to implement random advertisements on your website, using the AdRotator Control, and how to capture the number of clicks for each. VB version.

Random rotation of advertisements is made very easy in Visual Studio. This tutorial shows how to implement this control, and also a way of monitoring the number of clicks each ad receives.
This tutororial uses graphics to display clickable advertisements.

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Create New Folder in Solution Explorer for images. Copy to this folder existing images to be used as advertisements.
Then create an XML file to list the ads:
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>~/images/ad1.jpg</ImageUrl>
<NavigateUrl>http://www.dotnettutorials.com</NavigateUrl>
<AlternateText>Ad for DotNetTutorials.com</AlternateText>
</Ad>
<Ad>
<ImageUrl>~/images/ad2.jpg</ImageUrl>
<NavigateUrl>http://www.asp.net</NavigateUrl>
<AlternateText>Ad for ASP.NET Web site</AlternateText>
</Ad>
</Advertisements>

Create a web page to display the ads, add the AdRotator Control, and add the XML file (App_Data/Sample.ads) as the Data Source. The ASP should look something like this:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="XmlDataSource1" />
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Sample.ads">
</asp:XmlDataSource>
</div>
</form>
</body>
</html>

One way of tracking ad clicks is to write them into an XML file. To do this, you'll need to redirect the user to a page that counts the click before it redirects them to the advertisement website.

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Change NavigateUrl in the XML file (Sample.ads):

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>~/images/ad1.jpg</ImageUrl>
<NavigateUrl>AdRedirector.aspx?ad=ad1&amp;target=http://www.dotnettutorials.com</NavigateUrl>
<AlternateText>Ad for DotNetTutorials.com</AlternateText>
</Ad>
<Ad>
<ImageUrl>~/images/ad2.jpg</ImageUrl>
<NavigateUrl>AdRedirector.aspx?ad=ad2&amp;target=http://www.asp.net</NavigateUrl>
<AlternateText>Ad for ASP.NET Web site</AlternateText>
</Ad>
</Advertisements>

Now add another XML file - AdResponse.xml - to count the number of clicks.

<?xml version="1.0" standalone="yes"?>
<adResponses>
<ad adname="ad1" hitCount="0" />
<ad adname="ad2" hitCount="0" />
</adResponses>

Now create the redirection page, AdRedirector.aspx

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim adName As String = Request.QueryString("ad")
Dim redirect As String = Request.QueryString("target")
If (adName Is Nothing Or redirect Is Nothing) Then
redirect = "Default.aspx"
End If

Dim doc As System.Xml.XmlDocument
Dim docPath As String = "~/App_Data/AdResponses.xml"

doc = New System.Xml.XmlDocument()
doc.Load(Server.MapPath(docPath))
Dim root As System.Xml.XmlNode = doc.DocumentElement
Dim xpathExpr As String
xpathExpr = "descendant::ad[@adname='" & adName & "']"
Dim adNode As System.Xml.XmlNode = _
root.SelectSingleNode(xpathExpr)
If adNode IsNot Nothing Then
Dim ctr As Integer = _
CInt(adNode.Attributes("hitCount").Value)
ctr += 1
Dim newAdNode As System.Xml.XmlNode = _
adNode.CloneNode(False)
newAdNode.Attributes("hitCount").Value = ctr.ToString()
root.ReplaceChild(newAdNode, adNode)
doc.Save(Server.MapPath(docPath))
End If
Response.Redirect(redirect)

End Sub

To read the click data from XML, create another ASPX page - ViewAdData.aspx
Add XmlDataSource from the toolbox and choose AdResponses.xml (containing click data) as Data Source.
Also add GridView from toolbox and choose the Data Source. The code should be something like this:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="ViewAdData.aspx.vb" Inherits="ViewAdData" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Using the AdRotator Control and Counting Clicks</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/AdResponses.xml">
</asp:XmlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
<Columns>
<asp:BoundField DataField="adname" HeaderText="adname" SortExpression="adname" />
<asp:BoundField DataField="hitCount" HeaderText="hitCount" SortExpression="hitCount" />
</Columns>
</asp:GridView>
</form>
</body>
</html>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.



Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!