ASP.NET Animated Gifs and Long Running Processes

By Peter Bromberg

Shows a very easy-to-implement, cross-browser technique for displaying an animated progress gif while a long running method is going in your ASP.NET Page.

With all the new "AJAX" (Remote Scripting) and clientcallback methods available to ASP.NET developers, it has become a lot easier to refresh the DOM of a running page without a reload. However, oftentimes you do not want all the overhead of all that AJAX baggage, you just want to display an animated gif after the user clicks the button and have it display and animate until the page reloads, in order to display some sort of visual indication to the user  that something is happening.

The problem is, at least in Internet Explorer, this doesn't work at all  as advertised and we need to resort to some tricks to make it work in a true cross-browser fashion. This wasn't lost on fellow MVP Rick Strahl, and he has a whole blog page with lots of user comments on the subject.

The method I present here is the one that works best for me, and rather than launch into a big explanation, I think it would be easier just to show the code, as it is not very complex.

The key part of the HTML elements is as follows:

<span id='myHiddenDiv' style='display: none'>
<img src='' id='myAnimatedImage' align='absmiddle'>
</span>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="showDiv();"
Text="Search" />

We have a hidden span (it could be a div, if you want it on a new line), and inside this span is our img tag. Next, in the OnClientClick handler of our button, we call the "showDiv();" client script function which looks like so:

<script language='javascript'>
function showDiv()
{
document.getElementById('myHiddenDiv').style.display ="";
setTimeout('document.images["myAnimatedImage"].src="work.gif"', 200);
}
</script>



This uses the setTimeout method to make the DOM reset the src property of the image after 200ms. The result is that when you press your button that kicks off your long - running method, the image will show and continue animating until the postback returns and the page is reloaded, whereupon the image disappears -- exactly the behavior we want.

 

The download below is a "script only" single ASPX page that also includes an animated gif to use, so if you unzip this into your wwwroot folder and request it with http://localhost/workingimage.aspx you can test it out with no configuration, not even the need for a Visual Studio solution / project.

Thanks to Rick for featuring this information and also to the commenters who posted so much additional information.  Of course, you can use this same technique with other scripting languages and platforms, all you need to do is remove the OnClientClick ASP.NET attribute and instead, put an onlick="showDiv();" attribute right into your button's HTML code.

Download the zip file containing the example page and gif.

Popularity  (12459 Views)
Picture
Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Follow Microsoft MVP
Create New Account
Article Discussion: How to display an animated Gif during a long-running ASP.NET page call
Peter Bromberg posted at Monday, January 08, 2007 12:01 PM
reply
Typo?
Josh Stodola replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM

Hi Peter.

You said in the article "This uses the setTimeout method to make the DOM reset the src property of the image each 200ms."

That is not true.  That particular setTimeout() function call will make the DOM reset the src property only once (after waiting 200ms).  If it were to reset it each 200ms, you would be using the setInterval() function.

Hope this helps...

reply
Good catch.
Peter Bromberg replied to Josh Stodola at Friday, November 19, 2010 10:45 AM
I've changed the wording to "after" 200ms to match.
reply
Nice.. but creates problems with form validation.
Atif Iqbal replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM

I was looking for a solution that was simple since all i wanted was some kind of entertainment while the wait. Your solution works great and i think is a very good idea, and i used it on a couple of buttons. But i got stuck when i applied it to a button that also had some client side validation controls attached to it. So what happens is that the image starts rolling but under it, its showing me validation errors on form fields (which is expected). So i had to remove this solution from other buttons as well since i did not want to use multiple methods for the same task.

But thanks, this im sure will be a handy idea else where.

reply
Works well with setTimeout
Tim Bratcher replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM

I tried to do this on my own and ran into the problem of having the animation on the gif image freeze when the submit button was clicked.  I looked at a couple search results from Google and many people were suggesting the use of AJAX to solve this issue, but I needed something simpler, which is exactly what I found here.  I had all the pieces except the setTimeout line and when I added that it works great (even in IE 6). 

I've ran across some other posts by you and they are always helpful.  Thanks.

reply
Awesome!
Jason Gerstorff replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM

I spent a whole day trying to get an animated gif to work during an upload. I tried every combination of ajax and javascript i could think of with no success. We do have a separate page with an Active X progress indicator, but we also needed a simple progress indicator for a normal asp.net upload page.

I think this is the only page on the internet that accomplished the task! Thanks!!!!

reply
Michael replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM
This works great on a single page, but I can't seem to get it to work with a Master Page. Any idea what I'm missing? thanks!
reply
Peter Bromberg replied to Michael at Friday, November 19, 2010 10:45 AM
Michael,
You'd have to post some sample code.
reply
Michael replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM
Actually I did get it working! Thanks for replying though!

PAGE

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="WorkingImageWithMasterPage.aspx.vb" Inherits="WorkingImageWithMasterPage" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<script language='javascript'>

function showDiv()

{

document.getElementById('myHiddenDiv').style.display ="";

setTimeout('document.images["myAnimatedImage"].src = "spinner.gif"', 200);

}

</script>

 

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>

<span id='myHiddenDiv' style='display:none'>

<asp:Label ID="ProgressLabel" text="Loading the data, please wait..." runat="server" Height="42px" Style="z-index: 101; left: 238px; position: absolute; top: 255px" Width="364px"></asp:Label>

<img src='' id='myAnimatedImage' align='absmiddle'>

</span>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"

OnClientClick="showDiv();" Text="Search" />

<asp:Label ID="lblMessage" runat="server"></asp:Label>

 

</asp:Content>


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

System.Threading.Thread.Sleep(5000)

' this is a surrogate for your long-running method call

lblMessage.Text = "Done!"

End Sub


MASTER PAGE

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!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>Untitled Page</title>

<asp:ContentPlaceHolder id="head" runat="server">

</asp:ContentPlaceHolder>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

 

</asp:ContentPlaceHolder>

</div>

</form>

</body>

</html>





reply
m replied to Peter Bromberg at Friday, November 19, 2010 10:45 AM
Hi,
This is very good article but In my case I am using file upload control in asp.net and when click on browse button,at postback I read file content.I want that when postback starts that image should display and rotate.
I did it but image is showing but it does not rotate ? (Using gif file)
Please solve this issue...

reply
Chris replied to Michael at Friday, November 19, 2010 10:45 AM
Michael, did you ever solve this issue?

I'm experiencing the exact same problem as you.

Thanks

Chris.
reply