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 C#
Using the AdRotator Control in ASP.NET 2.0 and C#


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. 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.

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>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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>

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>

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.

Now create the redirection page, AdRedirector.aspx

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);
}

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.

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>

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

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks