……………………………………………….Expertise in .NET Technologies

  • Categories

  • Advertisements

AdRotator control

Posted by Ravi Varma Thumati on March 10, 2009

The AdRotator web server control provides a convenient way to display advertisements (ads) on your ASP.NET web pages. The control displays a graphic image that you provide—a .gif file or similar image. When users click the ad, they are redirected to a target URL that you have specified. The control automatically reads advertisement information, such as the graphic file name and the target URL, from a list of ads that you provide using a data source, which is usually an XML file or database table.

The AdRotator control selects ads randomly, changing the displayed ad each time the page is refreshed. Ads can be weighted to control the priority level of banners, making it possible to have certain ads display more often than others. You can also write custom logic that cycles through the ads.

Ad information can come from a variety of sources:

  • An XML file   You can store ad information in an XML file that contains references to ad banners and their associated properties.
  • Any data source control, such as the SqlDataSource control   For example, you can store ad information in a database, and can use a SqlDataSource control to retrieve ad information, and then bind the AdRotator control to the data source control.
  • Custom logic   You can create a handler for the AdCreated event and select an ad in the event.
One method of storing ad-banner image locations, URLs for redirection, and associated properties is to put the information in an XML file. By using the XML file format, you can create and maintain a list of advertisements without having to change the code in your application whenever a change is made to an advertisement.

The AdRotator control has the following attributes that you can specify in the XML file.

AdRotator attributes

  • ImageUrl   The URL of the image to display.
  • NavigateUrl   The URL of the page to go to when the AdRotator control is clicked.
  • AlternateText   The text to display if the image is unavailable.
  • Keyword   The category of the ad, which can be used to filter for specific ads.
  • Impressions   A numeric value (a weighting number) that indicates the likelihood of how often the ad is displayed. The total of all impressions values in the XML file cannot exceed 2,048,000,000 – 1.
  • Height   The height of the ad in pixels. This value overrides the default height setting for the AdRotator control.
  • Width   The width of the ad in pixels. This value overrides the default width setting for the AdRotator control.

To create an ad list as an XML file

  1. Create a new XML file in your website’s App_Data or _private folder. For extra security, give the file a file name extension other than .xml, such as .ads.
  2. Add the following XML elements to the file:
    <?xml version=”1.0″ encoding=”utf-8″ ?>
  3. </Advertisements>
  4. Create an Ad element inside the Advertisements element for each ad that you want to include in the ad list, and then save the file. A sample file might look like the following:
    <?xml version="1.0" encoding="utf-8" ?>
  5. <Advertisements xmlns="">
  6. <Ad>
  7. <ImageUrl>~/images/Contoso_ad.gif</ImageUrl>
  8. <NavigateUrl></NavigateUrl>
  9. <AlternateText>Ad for Contoso, Ltd. website</AlternateText>
  10. <Impressions>100</Impressions>
  11. </Ad>
  12. <Ad>
  13. <ImageUrl>~/images/Aspnet_ad.gif</ImageUrl>
  14. <NavigateUrl></NavigateUrl>
  15. <AlternateText>Ad for ASP.NET website</AlternateText>
  16. <Impressions>50</Impressions>
  17. </Ad>
  18. </Advertisements>

To display ads from the XML file

  1. In Design view, from the Toolbox, drag an AdRotator control to your the ASP.NET web page at the location where you want the ads to display.The Common AdRotator Tasks menu appears.
  2. Do one of the following:
    • In the Choose Data Source drop-down box, select the XML file you created.
    • In the Tag Properties task pane, set the AdRotator control’s AdvertisementFile property to the path of the XML file that you created.
Instead of creating an XML file for ad information, you can store ad information in a database table. The table requires a specific schema that the AdRotator control can read.

You can store ad information in any type of database, as long as you have the corresponding data source control for that database.

To create the database table for ads

  1. If you do not already have a database table that contains the ad information, create a table with the following schema. All columns except ID are optional.
    Data types are provided as SQL Server types. If you are using a different database, substitute the appropriate corresponding type.
    Column name Data type Description
    ID int The primary key. This column can have any name.
    ImageUrl nvarchar(length) The relative or absolute URL of the image to display for the ad.
    NavigateUrl nvarchar(length) The target URL for the ad. If you do not provide a value, the ad is not a hyperlink.
    AlternateText nvarchar(length) The text displayed if the image cannot be found. In some browsers, the text is displayed as a ToolTip. Alternate text is also used for accessibility so that users who cannot see the graphic can hear its description read out loud.
    Keyword nvarchar(length) A category for the ad on which the page can filter.
    Impressions int(4) A number that indicates the likelihood of how often the ad is displayed. The larger the number, the more often the ad will be displayed. The total of all impressions values in the XML file may not exceed 2,048,000,000 – 1.
    Width int(4) The width of the image in pixels.
    Height int(4) The height of the image in pixels.
  2. If you have an existing database table with ad information in it, set the following properties of the AdRotator control to map your database table schema to the fields required by the control:
    • AlternateTextField
    • ImageUrlField
    • NavigateUrlField
  3. Insert new records into the table with ad information.

To display ads from the database

  1. In Design view, from the Toolbox, drag a data source control, such as a SqlDataSource control or a AccessDataSource control, onto the ASP.NET web page.
  2. Configure the data source control with a connection string and query to select all of the records from the database table you created earlier.
  3. Place an AdRotator control on the page where you want the ads to appear.
  4. Set the AdRotator control’s DataSourceID property to the ID of the data source control that you created in step 1.

When the page runs, the AdRotator control will query the database for the ads and select one to display.

In the advertisement file, you can assign categories to ads by using the Keyword attribute and then configuring the AdRotator control to show ads according to the filter criteria that you specify. For example, if a specific ad file contains categories for both banks and hardware stores, you might only want the page to display ads that are related to banks. By setting the AdRotator control’s KeywordFilter property to “banks,” the hardware store ads will be filtered out.

If a filter is specified, then one of the following two situations can result:

  • If the AdRotator control can find ads with the matching keyword, an ad with the keyword is displayed.
  • If no match for the keyword exists, then the AdRotator control will display a blank image in the browser.
The AdRotator control does not inherently provide a way to gather statistics about which ads users have clicked, how many times an ad has been clicked, and so on. However, because you have control over the URL to which the ad points, you can add tracking logic for the ads to your application. A typical way to do this is to point all ads to a tracking page that first gathers the statistics you want, and then jumps to the ad’s destination page.
If the page that contains the AdRotator control is cached, the AdRotator control automatically performs cache substitution to replace the cached ad with a new one whenever the page is retrieved from the cache. For more information, see Dynamically Updating Portions of a Cached Page in the MSDN Library.

One Response to “AdRotator control”

  1. I merely wanted to inform you how much I appreciate everything you’ve shared to help improve lives of an individual in this subject material. Through your articles, I’ve really gone through just a novice to a specialist in the area. It is truly a honor to your efforts. Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: