Displaying Ads in ASP.NET using AdRotator Control

If you want to display Ads in your Asp.Net site, you can display by using AdRotator control.
AdRotator control is a Rich control in .Net Framework. It will display the ads by taking the inputs like image url, navigate url from the XML file or Database table.

Here I will explain about the Ad rotator control to display the ads by taking XML file as input.

Before going to the in details about AdRotator control, first we need to create XML file with required Ads.
Look at XML file below.

 
<?xml version="1.0" encoding="utf-8" ?> <Advertisements> <Ad> <ImageUrl>/images/MsBanner1.jpg</ImageUrl> <NavigateUrl>http://www.microsoft.com</NavigateUrl> <Keyword>leader</Keyword> <Impressions>100</Impressions> </Ad> <Ad> <ImageUrl>/images/MsBanner.jpg</ImageUrl> <NavigateUrl>http://www.microsoft.com</NavigateUrl> <Keyword>leader</Keyword> <Impressions>150</Impressions> </Ad> <Ad> <ImageUrl>/images/microsoft.jpg</ImageUrl> <NavigateUrl>http://www.microsoft.com</NavigateUrl> <Keyword>small</Keyword> <Impressions>150</Impressions> </Ad> <Ad> <ImageUrl>/images/sharepoint.jpg</ImageUrl> <NavigateUrl> http://www.microsoft.com/Sharepoint/default.mspx </NavigateUrl> <Keyword>small</Keyword> <Impressions>200</Impressions> </Ad> </Advertisements>

In the above, we use several XML tags. In those Advertisements is the root tag for remaning tags.
ImageUrl Tag will displays the path of the Image
NavigateUrl is the Navigation Url when user clicks on Ad
Keyword is the Keyword filter for AdRotator
Impressions is the how many impressions it needs to display.

Above XML file is the input file for the AdRotator control.

AdRotator Control has several properties.
In those AdvertisementFile requires the path of Ad XML file just you created.
KeywordFilter is Keyword for the Particular ads i.e, If you want to display only some ads at particular place , you can use KeywordFilter.


<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>AdRotator Control</title> <meta http-equiv="refresh" content="5" /> </head> <body> <form id="form1" runat="server"> <div> <div style="float:left;"> <asp:AdRotator ID="ad1" Target="_blank" AdvertisementFile="~/DisplayAd.xml" KeywordFilter="leader" runat="Server" /> </div> <div style="margin-top:100px;"> <asp:AdRotator ID="AdRotator1" Target="_blank" AdvertisementFile="~/DisplayAd.xml" KeywordFilter="small" runat="Server" /> </div> </div> </form> </body> </html>

In the above code, you can observe we have two AdRotator controls.
These have same XML file as AdvertisementFile DisplayAd.xml as input file but different KeywordFilter. First one has leader KeywordFilter and second one has small KeywordFilter. That means first one displays only those ads which has leader keyword and second one displays only yhose Ads which has small Keyword.

We have target property also for AdRotator. It describes value when user clicks on the where we need to display navigateurl website. Here we have _blank for Target that means we are opening the new window after user clicks on the Ad.

If you observe we have one meta tag in the head tage. That is “refresh” and it has 5 as content. This tag helps you to refresh the page depending on the content value. So, our ad page will refreshes for every 5 seconds because we gave the content value as 5.


Download source code here