Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Using the AdRotator Control in ASP.NET 2.0 and VB
Using the AdRotator Control in ASP.NET 2.0 and VB


ASP.NET Controls Tutorial

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.

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.aspnettutorials.com</NavigateUrl>
<AlternateText>Ad for ASPNETTutorials.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>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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>

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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.
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.aspnettutorials.com</NavigateUrl>
<AlternateText>Ad for ASPNETTutorials.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>

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!

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

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

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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>

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

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

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks