Today we will see how to make a slideshow with the adrotator control in two ways.
1. In a simple way
2. Using Ajax UpdatePanel and Timer Control to automatically update adrotator images.
- First create a new web site
- Create a XML File with the following contents:
<Advertisements>
<Ad>
<ImageUrl>../images/1.jpg</ImageUrl>
<Width>230</Width>
<Height>320</Height>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>First Image</AlternateText>
<Impressions>50</Impressions>
<Keyword>Banner</Keyword>
</Ad>
<Ad>
<ImageUrl>../images/2.jpg</ImageUrl>
<Width>230</Width>
<Height>320</Height>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Second Image</AlternateText>
<Impressions>40</Impressions>
<Keyword>Banner</Keyword>
</Ad>
<Ad>
<ImageUrl>../images/3.jpg</ImageUrl>
<Width>230</Width>
<Height>320</Height>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Third Image</AlternateText>
<Impressions>25</Impressions>
<Keyword>Banner</Keyword>
</Ad>
<Ad>
<ImageUrl>../images/4.jpg</ImageUrl>
<Width>230</Width>
<Height>320</Height>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Fourth Image</AlternateText>
<Impressions>60</Impressions>
<Keyword>Banner</Keyword>
</Ad>
<Ad>
<ImageUrl>../images/5.jpg</ImageUrl>
<Width>230</Width>
<Height>320</Height>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Fifth Image</AlternateText>
<Impressions>35</Impressions>
<Keyword>Banner</Keyword>
</Ad>
</Advertisements>
After that drag an adrotator control and xmldatasource control and bind the xmldatasource to the XML file then bind the XML file bound adrotator control to the xmldatasource so that it can fetch images from the datasource provided.
<%@ 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 id="Head1" runat="server">
<title>Adrotar Demo By Honey Chawla.</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="XmlDataSource1" />
<br />
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Ad.xml">
</asp:XmlDataSource><br />
<asp:HyperLink ID="NavHyperLink1" runat="server" NavigateUrl="~/AjaxAdrotator/AjaxAds.aspx">Click To See Ajax Adrotator Demo</asp:HyperLink>
</div>
</form>
</body>
</html>
Now run the application. The output will be:
Another Screenshot
If you want an adrotator to automatically switch images then you should use the following code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxAds.aspx.cs" Inherits="AjaxAdrotator_AjaxAds" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<p>
This is a Ajax Based SlideShow using ASP.Net Adrotator Control.
</p><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Timer ID="Timer1" Enabled="true" Interval="2000" runat="server"
ontick="Timer1_Tick"></asp:Timer>
<asp:AdRotator ID="AdRotator1" runat="server" DataSourceID="XmlDataSource1" />
<br />
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Ad.xml">
</asp:XmlDataSource>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
The output will be:
Another screenshot: