Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Flash ASP.NET AdRotator using Database

3.52/5 (14 votes)
5 Apr 2008CPOL3 min read 2   3.9K  
Implement a Flash ASP.NET AdRotator to fetch ads from a database.

Adrotator.JPG

Introduction

This article explains the concept of the ASP.NET AdRotator control fetching ad information for Flash from a database and rotating the ads with Previous/Next button clicks. This article also explains how to store Flash (.swf) files in a database and display the ads in a specific order.

Advantages of using a database

  • Instead of storing files in XML, the database is easier to maintain ads.
  • Easy to update or maintain the active list of ads using a flag in the database.
  • Add any number fields to store specific/company information about the ads.

Understanding the AdRotator

You can use the AdRotator control to display a randomly selected advertisement banner on a Web page. The displayed advertisement changes whenever the page refreshes.

Advertisement information is stored in a separate XML file. The XML file allows you to maintain a list of advertisements and their associated attributes. Attributes include the path to an image to display, the URL to link to when the control is clicked, the alternate text to display when the image is not available, a keyword, and the frequency of the advertisement. Information in this file is not validated by the AdRotator control. To prevent ads from executing malicious scripts, you should always check the data before releasing it, or accept ad information only from trusted sources.

The XML file contains the following predefined attributes. Only the ImageUrl attribute is required.

AttributeDescription
ImageUrlThe URL of the image to display.
HeightThe height of the image, in pixels (optional).
WidthThe width of the image, in pixels (optional).
NavigateUrlThe URL of the page to navigate to when the AdRotator control is clicked.
AlternateTextThe text to display if the image is unavailable. On some browsers, this text is displayed as a ToolTip.
KeywordThe category for the advertisement. This is used by the AdRotator control to filter the list of advertisements for a specific category.
ImpressionsA value that indicates how often an advertisement is displayed in relation to other advertisements in the XML file.

Use the above attributes to create fields in the database.

SQL
Create Table AdsDataTable 
( 
  OrderId int,
 br br br br br br br br br br   AlternateText VarChar(200), 
  ImageUrl VarChar(200), 
  NavigateUrl VarChar(200),
  IsAdActive bit 
)

You can create as many number of fields to store metadata...for my use, I have created only the above fields.

Implementation

The basic AdRotator control in .NET does not have support for Flash. So I have modified it to support Flash. The DLL I am using, AdRotator.dll, is included in the sample bin directory, with source code.

The AdRotator uses myAds.xml as the data source which is an XML file...

ASP.NET
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/myAds.xml"/>

Replace AdvertisementFile with DataSource in the code-behind file Page_Load event:

C#
FlashAdRotator1.DataSource = GetAds(AdCount); 
FlashAdRotator1.DataBind();

Create Next and Previous buttons....

C#
protected void NextBtn_Click(object sender, ImageClickEventArgs e)
{
   if (ViewState["Count"]!= null) 
   AdCount = (int)ViewState["Count"];
   AdCount = AdCount + 1; 
   ViewState["Count"]= AdCount; 
   NextBtn.Enabled = AdCount == TotalAds ? false : true; 
   PrevBtn.Enabled = AdCount >= 1 ? true : false; 
   FlashAdRotator1.DataSource = GetAds(AdCount);
   FlashAdRotator1.DataBind(); 
}

protected void PrevBtn_Click(object sender, ImageClickEventArgs e)
{
  if (ViewState["Count"] != null)
     AdCount = (int)ViewState["Count"];
  AdCount = AdCount - 1; 
  ViewState["Count"] = AdCount; 
  PrevBtn.Enabled = AdCount == 1 ? false : true; 
  NextBtn.Enabled = AdCount <= TotalAds ? true : false; 
  FlashAdRotator1.DataSource = GetAds(AdCount); 
  FlashAdRotator1.DataBind(); 
}

Fetch ads from the database:

C#
private DataTable GetAds(int AdNo)
{ 
 string sql = "select AlternateText,ImageUrl, " + 
              "NavigateUrl from AdsDataTable where OrderId = " + 
              AdNo.ToString(); 
 SqlConnection con = new SqlConnection(
   ConfigurationManager.ConnectionStrings["AdsConnectionString"].ToString()); 
 SqlDataAdapter adsa = new SqlDataAdapter(sql, con);
 DataTable AdsTable = new DataTable(); adsa.Fill(AdsTable);
 return AdsTable;
}

This way, users can make use of a database to store ads and categorize them based on their preference. Also, if you don't like to click, you can add a timer control to this and set the seconds to rotate the ads automatically.

Platform/Installation

The sample code will work in VS2005 with AJAX. Also I have attached the sample database MDF/LDF file.

History

  • April 5th 2008. Initial version published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)