How to Bind Use ASP.NET AdRotator Control in C# VB.Net

In recent days rising of eCommerce, affiliation program, internet marketing, site hosting , pay per click , freelance advertising banners have played a lucrative role of primary or additional income source for many web sites. So in one sentence we can say that the AdRotator control of ASP.Net is used to display a sequence of ad images which is one of the built in ASP.Net server control. If you need to generate more ads in a single place in your web page you may think about AdRotator. Because the AdRotator feeds sequence of ads with its corresponding URL. You could use the target property within the AdRotator control for the destination page opener. If you set target=’blank’ then ads will be displayed in a new browser otherwise within the container page.

Since the AdRotator server control use XML file to display the ads so you have give the first look on how you can generate XML file for AdRotator. Here i will describe the format of XML:

*** Must begin and end with an tag.
*** Under tag there may be one or more tags which represents a single ad.
*** Under tag you can define the below elements:

Element Required? Description
<imageurl> No Image location
<navigateurl> No URL to link to the ad
<alternatetext> No Image alternate text
<impressions> No Display ratio based on hits

So first add an XML file and named it test_adrotator.xml:

<?xml version="1.0" encoding="utf-8" ?>
    <AlternateText>This is an adrotator demo 1</AlternateText>
    <AlternateText>This is an adrotator demo 2</AlternateText>

Note: Create a folder in root with “Ads” name & copy your images in this folder.

Now Add an Adrotator Control in your page & Bind with the XML file:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:adrotator id="first_adrotator" runat="server" AdvertisementFile="test_adrotator.xml" borderwidth="2"/>

Now run the project hope you will get below output:
How to use ASP.NET AdRotator Control in CSharp VB.Net

One very cool feature of AdRotator control is ‘Keyword’. You can set the KeywordFilter property to show only Ads that match the keyword you specify in the XML file. Lets you have two pages one is for cosmetics & other is for sunglasses. So definitely you want to display cosmetics related ads in your cosmetics page. So you have to add the KeywordFilter=”Cosmetics” in the AdRotator control like:

<asp:adrotator id="firstadrotator" runat="server" advertisementfile="testadrotator.xml" borderwidth="2" keywordfilter="Cosmetics" />

The above AdRotator will display only cosmetics Ads. So i think basic understanding is clear. Now i am looking for how we can dynamically generate the C# VB.Net server side control AdRotator & change configuration xml file. To do that you have to add a Panel in your page & runtime add the control in the panel container by just 3 lines like:

C# Code:

AdRotator firstadrotator = new AdRotator();
// You can change dynamically xml file from here
firstadrotator.AdvertisementFile = "test_adrotator.xml";

VB.Net Code:

Dim firstadrotator As New AdRotator()
' You can change dynamically xml file from here
firstadrotator.AdvertisementFile = "test_adrotator.xml"

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

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