Background
AdRotator is widely used in today's business to display advertisements in the form of graphics, images, event details and so on to provide summary details about specific things. In addition to that AdRotator is used for many other purposes depending on business needs. Now let us learn how to use AdRotator step-by-step.
The following are the few properties that are commonly used.
Advertisement File: This is used to specify the advertisement file details that are mostly in the form of XML.
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".
- "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. Then the page will look such as follows.
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 Solution Explorer will look as in the following:
We have used the Ajax Update Panel with Timer control to auto refresh the advertisement file 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 2000 mili-seconds, then the second advertisement file will be automatically shown because we used the Timer control and specified a Time Interval of 2000. So after 2000 mili-seconds the next advertisement file is displayed as:
Now if a click above the advertisement is made then the file will navigate you to the C# Corner Home page website.
Note
- Download the Zip file from the attachment for the full source code of the application.
- We can also provide the advertisement file using a Data Source.
Summary
I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.