AdRotator Control In ASP.NET

In this article, we will learn about how we can use AdRotator control in ASP.NET.

The AdRotator control is used to randomly selects banners graphics from a list, which is specified in an external XML (Extensible Markup Language) file. This external XML schedule file is called an Advertisement File.

AdRotator is a server-side control, To use AdRotator we can drag it from the toolbox of visual studio or ASP.NET provides its own tag to use AdRotator. The example is given below.

<asp:AdRotator ID="AdRotator1" runat="server" />

The AdRotator control has its own properties that are tabled below.

Property Description
AdvertisementFile The path to the advertisement file.
AlternateTextFeild The element name of the field where the alternate text is provided. The default value is AlternateText.
DataMember The name of the specific list of data to be bound when the advertisement file is not used.
DataSource Control from where it would be able to retrieve data.
DataSourceID Id of the control from where it would be able to retrieve data.
ImageUrlField The element name of the field where the URL for the image is provided. The default value is ImageUrl.
KeywordFilter For displaying the keyword-based ads only.
NavigateUrlField The element name of the field where the URL to navigate to is provided. The default value is NavigateUrl.
Target The browser window or frame that displays the content of the linked page.

The Advertisement File

The advertisement file is a .xml file, which contains information about the advertisements to be displayed. It is a text-based markup language that enables you to store data in a structured format by using meaningful tags. XML is not a language in itself. It is a meta-markup language. For special uses It allows developers to create custom tag sets. It structures, stores, and transports the information.

The standard XML elements that are commonly used in the advertisement file are tabled below.

Property Description
Advertisements Encloses the advertisement file.
Ad Delineates separate ad.
ImageUrl The path of image that will be displayed.
NavigateUrl The link that will be followed when the user clicks an ad.
AlternateText The text that will be displayed instead of the picture if it cannot be displayed.
Keyword Keyword identifying a group of advertisements. This is used for filtering.
Impressions The number that indicates how often an advertisement will appear.
Height Height of the image to be displayed.
Width Width of the image to be displayed.

Example

In this example, we are implementing an AdRotator control in ASP.NET.

Create a new project and select the ASP.NET Empty Web Site.

Now, right-click the project name (CRUD Operations) in the Solution Explorer and select Add -> Add New Item.

First, add a new XMLFile.xml file (Advertisement File), select XML File and click Add.

Then add images to the project which we want to use in the Advertisement File.

Open the XMLFile.xml file and add the code in it.

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <ImageUrl>Ad1.jpg</ImageUrl>
    <NavigateUrl>https://en.wikipedia.org/wiki/Flower</NavigateUrl>
    <AlternateText>
      Flowers
    </AlternateText>
    <Impressions>10</Impressions>
    <Keyword>flowers</Keyword>
  </Ad>

  <Ad>
    <ImageUrl>Ad2.jpg</ImageUrl>
    <NavigateUrl>https://en.wikipedia.org/wiki/Sport_bike</NavigateUrl>
    <AlternateText>Bike</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>bike</Keyword>
  </Ad>

  <Ad>
    <ImageUrl>Ad3.gif</ImageUrl>
    <NavigateUrl>https://en.wikipedia.org/wiki/Loader</NavigateUrl>
    <AlternateText>Loader</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>loader</Keyword>
  </Ad>
</Advertisements>

Now, let’s add a new Default.aspx file, right-click the project name (CRUD Operations) in the Solution Explorer and select Add -> Add New Item, select Web Form and click Add.

Open the Default.aspx file and add the code in it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/XMLFile.xml" Target="_blank" />
        </div>
    </form>
</body>
</html>

That’s it.

Output:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories