AdRotator Control in ASP.NET

 

AdRotator control is used to display different advertisements in Asp.Net. This control displays the ads one by one dynamically based on its impressions value.

 

AdRotator control takes the XML file as input. In XML file we have to mention the advertisement details. So add Advertisements.xml as shown below.

 

<?xmlversion="1.0"encoding="utf-8" ?>

<Advertisements>

  <Ad>

    <ImageUrl>images/office.jpg</ImageUrl>

    <NavigateUrl>http://www.microsoft-csharp-tutorials.com</NavigateUrl>

    <Keyword>microsoft</Keyword>

    <Impressions>100</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>images/windows8.jpg</ImageUrl>

    <NavigateUrl>http://www.microsoft-csharp-tutorials.com</NavigateUrl>

    <Keyword>microsoft</Keyword>

    <Impressions>200</Impressions>

  </Ad>

  <Ad>

    <ImageUrl>images/xbox-360-console.jpg</ImageUrl>

    <NavigateUrl>http://www.microsoft-csharp-tutorials.com</NavigateUrl>

    <Keyword>microsoft</Keyword>

    <Impressions>150</Impressions>

  </Ad>

</Advertisements>

 

As shown above Advertisements.xml have different Ads with root elemnt as “Advertisements” and each ad in “Ad” tag. Each ad contains ImageUrl, NavigateUrl, Keyword and Impressions elemnts. Keyword tag is used to filter the ads.

Now add AdRotator control to asp.net page and displays ads from Advertisements.xml file as shown below.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AspNetAdRotatorExp.WebForm1" %>

 

<!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>AdRotator Control in Asp.Net</title>

    <meta http-equiv="refresh" content="5" /> 

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:AdRotator AdvertisementFile="~/Advertisements.xml" ID="AdRotator1"

         KeywordFilter="microsoft" Target="_blank"  runat="server" />

    </div>

    </form>

</body>

</html>

 

Add meta tag to refresh the page, so it displays the different ads for every 5 seconds. As shown above we provided the xml file path to AdvertisementFile attribute. We can have different value for KeywordFilter to filter the ads from Advertisements.xml file.

 

When you run the application, the output is display as shown below.

 

                                                                                  AspNetAdRotatorExp.zip (114.31 kb)