

Navigator : Home > Tutorials > Controls Tutorials > ...
Using the AdRotator Control in ASP.NET 2.0 and C#
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. C# 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 tutorial uses graphics to display clickable advertisements.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
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="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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 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.
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&target=http://www.dotnettutorials.com</NavigateUrl> <AlternateText>Ad for DotNetTutorials.com</AlternateText> </Ad> <Ad>
<ImageUrl>~/images/ad2.jpg</ImageUrl> <NavigateUrl>AdRedirector.aspx?ad=ad2&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
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.
protected void Page_Load(object sender, EventArgs e) {
String adName = Request.QueryString["ad"]; String redirect = Request.QueryString["target"]; if (adName == null | redirect == null)
redirect = "Default.aspx"; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); String docPath = @"~/App_Data/AdResponses.xml"; doc.Load(Server.MapPath(docPath)); System.Xml.XmlNode root = doc.DocumentElement; System.Xml.XmlNode adNode =
root.SelectSingleNode( @"descendant::ad[@adname='" + adName + "']"); if (adNode != null) {
int ctr = int.Parse(adNode.Attributes["hitCount"].Value); ctr += 1; System.Xml.XmlNode newAdNode = adNode.CloneNode(false); newAdNode.Attributes["hitCount"].Value = ctr.ToString(); root.ReplaceChild(newAdNode, adNode); doc.Save(Server.MapPath(docPath)); } Response.Redirect(redirect); } |
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="C#" AutoEventWireup="true" CodeFile="ViewAdData.aspx.cs" 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> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!