Background
AdRotator is widely used on websites to display banner ads. In this article, let's learn how to use Ad Rotator in ASP.NET.
The following few properties are commonly used in Ad Rotator.
Advertisement File: An XML file that stores all ads and their details.
AlternateText Field: This property determines which text to be displayed when an Image is not available on AdRotator for technical reasons.
ImageUrl Field: URL of the Image that is to be displayed using the AdRotator.
Keyword Filter: This property filters the advertisement using a specific keyword.
NavigateUrl Field: This is the URL that navigates you to a specific location after clicking the advertisement.
Now let us create the application to demonstrate the use of the AdRotator control.
Use the following procedure to create a web site:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010". You can use any later versions of Visual Studio.
- "File" - "New" - "Website..." then select "C#" - "Empty Project" (to avoid adding a master page).
- Provide the project a name such as "UsingAdRotator" or another as you wish and specify the location.
- Then right-click on Solution Explorer then select "Add New Item" - "Default.aspx" page.
- Create one folder by right-clicking on Solution Explorer to store Images.
- Add XML file by right-clicking on Solution Explorer to store Advertisement details.
- Drag and Drop one AdRotator control onto the Default.aspx page.
Now create a XML file to store the advertisement details that will look as in the following:
- <?xml version="1.0" encoding="utf-8" ?>
- <Advertisements>
- <Ad>
- <ImageUrl>~/Banners/comp.jpg</ImageUrl>
- <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>
- <AlternateText>C# Corner</AlternateText>
- </Ad>
- <Ad>
- <ImageUrl>~/Banners/Csharpcorner.jpg</ImageUrl>
- <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>
- <AlternateText>C# Corner Learn Asp.net</AlternateText>
- </Ad>
- <Ad>
- <ImageUrl>~/Banners/Csharpcorner.jpg</ImageUrl>
- <NavigateUrl>http://www.c-sharpcorner.com/</NavigateUrl>
- <AlternateText>C# Corner</AlternateText>
-
- </Ad>
- </Advertisements>
Our final Solutionr will look as in the following:
We have used an Ajax Update Panel with a Timer control to auto refresh the ads without an entire page refresh, so after adding all the required controls our Solution Explorer will look like as follows:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!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></title>
- </head>
- <body bgcolor="gray">
- <form id="form1" runat="server">
- <h2 style="color: #800080">
- Our Clients</h2>
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server" />
- <asp:Timer ID="Timer1" Interval="2000" runat="server" />
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
- </Triggers>
- <ContentTemplate>
- <asp:AdRotator Height="150px" Width="600px" ID="AdRotator1" AdvertisementFile="~/App_Code/ClientsXMLFile.xml"
- runat="server" />
- </ContentTemplate>
- </asp:UpdatePanel>
- </div>
- </form>
- </body>
- </html>
Now run the application, the output will look like as follows:
Now wait for 2 seconds, then the second banner will be displaed automatically because we used a Timer control and specified a Time Interval of 2000.
Now if a click on the above banner, it will navigate to C# Corner Home page.
Note
- Download the Zip file from the attachment for the full source code of the application.
- We can also provide the ad details file using a Data Source.
Summary
In this article, we learned use of an ASP.NET AdRotator control. Here is a detailed tutorial for more details:
Implement Ad Rotator in ASP.NET