Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 352, comments - 144, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

ASP.NET AJAX Random Image Rotator

Joe Healy (http://www.devfish.net) came to Orlando today to talk about ASP.NET AJAX (http://ajax.asp.net). After seeing his presentation I was inspired to play with the tools. Additionally I got an email asking that I build an image rotator that changed the images on a webpage every 5 seconds. I thought to myself, self I can do that with AJAX, so I did, and I thought I would share it with you.

First I added a ScriptManager and UpdatePanel to the page, inside the ContentTemplate area of the UpdatePanel I added an AJAX Timer and an ASP image. Next I set the interval property to the number of milliseconds I wanted each picture to be visible. Now in the code behind I created a method that set the url property of the image to a random image from my images folder. I now call that method from the page load event and like magic you have a simple random image rotator in ASP.NET AJAX.

Thanks Joe for coming out to speak to ONETUG!

1
2
3
4
5
6
7
8
9
10
11
12
    <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server"
Interval="5000">
</asp:Timer>
<asp:Image runat="server" ID="img" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
    protected void Page_Load(object sender, EventArgs e)
{
SetNextImage();
}
private void SetNextImage()
{
string path = Server.MapPath("~/Image/");
string[] files = Directory.GetFiles(path);
Random r = new Random();
FileInfo fi = new FileInfo(files[r.Next(0, files.Length)]);
img.ImageUrl = "~/Image/" + fi.Name;
}

Print | posted on Saturday, December 02, 2006 12:06 AM | Filed Under [ .NET ASP.NET C# ]

Feedback

Gravatar

# re: ASP.NET AJAX Random Image Rotator

Pragnesh, Looks like tdinardo has done this with AdRotator. (drowningintechnicaldebt.com/.../...ge-Rotator.aspx)
10/27/2008 4:44 PM | sweisfeld

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 3 and type the answer here:

Powered by: