Create new account I forgot my password    

How to Rotate Ads without Refreshing the Page using AdRotator and ASP.NET AJAX
Rating: 28 user(s) have rated this article Average rating: 4.2
Posted by: Suprotim Agarwal, on 9/17/2008, in category "ASP.NET AJAX"
Views: this article has been read 22180 times
Abstract: The AdRotator control in ASP.NET is extremely handy when it comes to randomly displaying advertisements on your site. However the ads are rotated only when the user refreshes the page. In this article, we will explore how you can easily rotate ads at regular intervals, without the user refreshing the page.

How to Rotate Ads without Refreshing the Page using AdRotator and ASP.NET AJAX
 
The AdRotator control in ASP.NET is extremely handy when it comes to randomly displaying advertisements on your site. However the ads are rotated only when the user refreshes the page. In this article, we will explore how you can easily rotate ads at regular intervals, without the user refreshing the page. Follow these steps:
Step 1: Open VS 2008. Click File > New > Website. Choose ASP.NET Website from the list of installed template, choose target platform as .NET Framework 3.5, choose the desired language and enter the location where you would like to store the website on your FileSystem. I have created a folder called VS2008 Projects, so the location over here is C:\VS 2008 Projects\AdRotatorAJAX. After typing the location, click OK.
Step 2: Now you would need to create some banners images to test out the adrotator functionality. I have designed three 270 * 270 banners (dnc.jpg, ssc.jpg and writeforus.jpg)  that has been attached along with the code over here. Let us add them to our project. Right click the project > New Folder > Rename it to ‘images’. Now add the 3 banners images to this project.
Step 3: The AdRotator control can retrieve the list of advertisements from either a XML file or from the database. To keep things simple, I will be using an XML file. So the next step is to create the advertisement file. I prefer to create this file in the App_Data folder as it is secure. To do so, right click the App_Data folder > Add New Item > ‘XML File’ > Rename it to ads.xml and click Add. Now add the following contents to the ‘ads.xml’ file:
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
 <Ad>
    <ImageUrl>~/images/dnc.jpg</ImageUrl>  
    <NavigateUrl>http://www.dotnetcurry.com</NavigateUrl>
    <AlternateText>DotNetCurry Home Page</AlternateText>
    <Impressions>40</Impressions>
    <Keyword>small</Keyword>
 </Ad>
 <Ad>
    <ImageUrl>~/images/ssc.jpg</ImageUrl>
    <NavigateUrl>http://www.sqlservercurry.com</NavigateUrl>
    <AlternateText>SQL Server Curry Home Page</AlternateText>
    <Impressions>20</Impressions>
    <Keyword>small</Keyword>
 </Ad>
 <Ad>
    <ImageUrl>~/images/writeforus.jpg</ImageUrl>
    <Width>300</Width>
    <Height>50</Height>
    <NavigateUrl>http://www.dotnetcurry.com/writeforus.aspx</NavigateUrl>
    <AlternateText>dotnetcurry.com Write For Us</AlternateText>
    <Impressions>40</Impressions>
    <Keyword>small</Keyword>
 </Ad
</Advertisements>
Step 4: Our next step is to add the AdRotator control and bind it to the advertisement file. Drag and drop an AdRotator control from the toolbox to the Default.aspx. To bind the AdRotator to our XML file, we will make use of the ‘AdvertisementFile’ property of the AdRotator control as shown below:
<asp:AdRotator
    id="AdRotator1"
    AdvertisementFile="~/App_Data/Ads.xml"
    KeywordFilter="small"
    Runat="server" />
Note: The ‘KeywordFilter’ property enables you to filter advertisement using a keyword. If your Advertisement file contains different kinds of ads (banner, leaderboard, skyscraper etc.), you can use this property to filter out different ads on different sections of the page. If you observe, the ads.xml file also contains a property called ‘Keyword’ which binds that ad with the AdRotator that contains the same KeywordFilter, in our case ‘small’.
If you run the sample now, you will have to refresh the page to display a new advertisement. In the last step, let us see how to rotate the ads without refreshing the page.
Step 5: To rotate the ads without refreshing the page, we will add some AJAX code to the page. The trick is to drop the AdRotator in an UpdatePanel and then use a Timer control to refresh the contents at regular intervals. The resultant code will be as shown below:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer ID="Timer1" Interval="2000" runat="server" />
 
    <asp:UpdatePanel ID="up1" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:AdRotator
            id="AdRotator1"
            AdvertisementFile="~/App_Data/Ads.xml"
            KeywordFilter="small"
            Runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>   
</div>
</form>
If you observe, we have used Triggers inside an UpdatePanel. Triggers enable the UpdatePanel to refresh its content from a control outside the UpdatePanel. In our case, the Timer control is the control outside the UpdatePanel, which updates the content of the UpdatePanel. The content here is the AdRotator control.
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
The Event captured is the ‘Tick’ event of the Timer control. So every time the Timer control raise a tick event, an async postback occurs and the contents of the UpdatePanel is refreshed and that is how you are able to see the AdRotator control refresh its content at regular intervals.
So that was a quick tip on how to rotate ads using the AdRotator at regular intervals without refreshing the page. The source code of the article can be downloaded from here. I hope this article was useful and I thank you for viewing it.







Follow me on twitter

Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Pradeep Kumar Mishra on Monday, September 22, 2008 2:36 AM
nice and useful article. Thanks for putting this up.
Comment posted by sasi on Monday, October 06, 2008 6:14 AM
Need some that can be understand by the beginerrs
Comment posted by jjohn on Thursday, October 16, 2008 3:51 PM
Good stuff! Thanks.
Comment posted by Vivek Vashisht on Saturday, November 01, 2008 2:05 AM
Nice one. I used the same concept but with timer control inside update panel and without triggers.
Comment posted by Thanigainathan.S on Saturday, November 01, 2008 4:12 AM
Hei this is really a nice article.
Thanks for the good stuff.
Comment posted by Allen on Sunday, November 02, 2008 8:10 PM
How you can have fade in/out effort on this control?
Comment posted by Ek Gunawan on Sunday, November 02, 2008 10:13 PM
Hi there,

Does any one know how to bind this with dataset or not using xml. Using data from .mdb for example.
Comment posted by sukeev on Monday, November 03, 2008 9:50 AM
useful stuff. Thx
Comment posted by Scott Sinclair on Tuesday, November 04, 2008 4:41 AM
Great article, keep up the good work.

Thanks

Scott
Comment posted by Muhammad Yasar Ikram on Tuesday, November 04, 2008 5:31 AM
great article


thanx for sharing a really nice n informative article.
Comment posted by vinit sambhare on Wednesday, November 26, 2008 6:49 AM
Good One !
Also please mention something about master pages too.
Comment posted by apoorv mittal on Monday, December 08, 2008 11:50 PM
hiiiii
please tell me how to move links in a panel from down to up as we can see websites contain the news heading in it.
as i have learned to rotate the images with the help of AJAX Update Panel and XML file and AD Rotater.
I also want to ask what are the other ways to use XML file in an visual studio.
Comment posted by lion.jack on Tuesday, December 09, 2008 4:41 AM
Good........
Comment posted by Suprotim Agarwal on Tuesday, December 09, 2008 8:59 AM
Thanks everyone for your comments!

Vinit: Sure. I will be posting an article soon which will contain similar stuff using Master Pages

Apporv: I am not sure if I understand you, but you could try out the following. Create a key value collection and bind your control with the coll. Keep the keys from say 1 to 5. Just shuffle the keys and rebind the collection and you will get what you want.
Comment posted by Amol on Thursday, January 15, 2009 5:36 AM
But still page is postback so please help how to avoid the auto post abck while using adrotator
Comment posted by Rashmi on Wednesday, April 15, 2009 5:53 AM
simply cool
Comment posted by skaufmann on Friday, May 08, 2009 3:05 PM
How can I get the password to open File????
Comment posted by chint_1000 on Tuesday, June 02, 2009 2:10 AM
i downloaded this article. On compiling i get error of unknown server tag asp:ScriptManager,asp:Timer,asp:UpdatePanel. Pls let me know workaround for this.
Comment posted by Suprotim Agarwal on Thursday, June 04, 2009 3:02 AM
Chint_1000: Is your site ASP.NET AJAX enabled?
Comment posted by harvinder singh syan on Monday, June 15, 2009 3:25 AM
really very effective code it is
Comment posted by Ramani Sandeep on Friday, July 17, 2009 10:13 AM
i hv tried this code in my user control but still my page got refreshed after few seconds.
main problem is on data entry form , when ad changes , all the data filled got erased as page refresh.. i dont knw where is the problem...
Comment posted by kavya on Thursday, October 29, 2009 4:10 AM
how to bind this with database  or not using xml.
Comment posted by Manimaran,V on Tuesday, November 24, 2009 1:29 AM
Really very useful to everyone.. I thank to this author. because i searched many sites but they not given properly so it's difficult to understand. so this Project is given step by step  is very good.. keep it up.

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel