ASP.NET AJAX: Rendering a Gmail-like 'Loading' Indicator over a Specific ASP.NET Control
I came across a blog post discussing a slick way to use the ASP.NET AJAX UpdateProgress control to add a gmail-like progress indicator to an ASP.NET web page.

I liked the approach the author used and it clearly works great for scenarios where you want to display a single 'something is happening' message to the user while the page is being updated. However, after the operation completes and the indicator disappears, it is still up to the user to figure out where on the screen (i.e. what control was updated?) to look for the new changes. If your screens are relatively simple, or if you feel the update should be obvious this is probably not an issue for you. Unfortunately things aren't so for the web application I am currently working on. A few of the screens contain a number of different sections and users would like to see the indicator rendered over the control (most commonly a GridView or DetailsView) that is being updated.
To achieve this, I am using the UpdatePanelAnimationExtender control that is part of the AjaxControlToolkit (You can follow directions here to install and get started using the toolkit). This control allows for defining the visual effects you want run before (the OnUpdating animation) and after (the OnUpdated animation) the contents within the UpdatePanel have been refreshed. For the gmail progress indicator, the OnUpdaing animation runs a piece of JavaScript that calculates the bounds of the GridView that is contained within the UpdatePanel and renders an HTML DIV in the upper right hand corner of the GridView control. After the update occurs, the OnUpdated fires and runs another piece of JavaScript that hides the DIV.
You can view a live demo here and download the sample web application with all of the code included here. There is no code-behind for the page, so if you are intereseted in quickly viewing the markup and JavaScript for the page - it is posted below (you will have to change the connection string to point to your copy of the Northwind database ...)
Code sample for this page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> <!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>Untitled Page</title> <script type="text/javascript" language="javascript"> function onUpdating(){ // get the update progress div var updateProgressDiv = $get('updateProgressDiv'); // get the gridview element var gridView = $get('<%= this.gvCustomers.ClientID %>'); // make it visible updateProgressDiv.style.display = ''; // get the bounds of both the gridview and the progress div var gridViewBounds = Sys.UI.DomElement.getBounds(gridView); var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv); var x; var y; // do the math to figure out where to position the element if($get('rdoCenter').checked){ // center of gridview x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2); y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2); } else if($get('rdoTopLeft').checked){ // top left of gridview x = gridViewBounds.x; y = gridViewBounds.y; } else{ // top right of gridview x = (gridViewBounds.x + gridViewBounds.width - updateProgressDivBounds.width); y = gridViewBounds.y; } // set the progress element to this position Sys.UI.DomElement.setLocation (updateProgressDiv, x, y); } function onUpdated() { // get the update progress div var updateProgressDiv = $get('updateProgressDiv'); // make it invisible updateProgressDiv.style.display = 'none'; } </script> </head> <body> <form id="form" runat="server"> <asp:ScriptManager ID="scriptManager" runat="server" /> <div> <asp:SqlDataSource ID="sqldsCustomers" runat="server" SelectCommand="select customerid, companyname, contactname, contacttitle from dbo.customers" SelectCommandType="Text" ConnectionString="todo" /> <p style="background-color:AliceBlue; width:95%"> Example of using an UpdatePanelAnimationExtender to place an animated gif over a GridView while the<br /> GridView is being refreshed. Click the column headers or the paging buttons to cause the grid to refresh<br /> You can toggle where the animation is to be displayed using the radio buttons<br /> </p> <div> <input id="rdoCenter" type="radio" name="location" value="center" checked="checked" />Center <input id="rdoTopLeft" type="radio" name="location" value="topleft" />Top Left <input id="rdoTopRight" type="radio" name="location" value="topright" />Top Right </div> <br /> <br /> <asp:Label ID="lblTitle" runat="server" Text="Customers" BackColor="lightblue" Width="95%" /> <asp:UpdatePanel ID="updatePanel" runat="server"> <ContentTemplate> <asp:GridView ID="gvCustomers" runat="server" AllowPaging="true" AllowSorting="true" PageSize="20" DataSourceID="sqldsCustomers" Width="95%"> <AlternatingRowStyle BackColor="aliceBlue" /> <HeaderStyle HorizontalAlign="Left" /> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> <ajaxToolkit:UpdatePanelAnimationExtender ID="upae" BehaviorID="animation" runat="server" TargetControlID="updatePanel"> <Animations> <OnUpdating> <Parallel duration="0"> <%-- place the update progress div over the gridview control --%> <ScriptAction Script="onUpdating();" /> </Parallel> </OnUpdating> <OnUpdated> <Parallel duration="0"> <%--find the update progress div and place it over the gridview control--%> <ScriptAction Script="onUpdated();" /> </Parallel> </OnUpdated> </Animations> </ajaxToolkit:UpdatePanelAnimationExtender> <div id="updateProgressDiv" style="background-color:#CF4342; display:none; position:absolute;"> <span style="color:#fff; margin:3px">Loading ...</span> </div> </div> </form> </body> </html>
Comments
Nicely done, I have a different method of doing this and I think it's easier/less code (I am lazy)... Check it out at http://www.emadibrahim.com/2007/08/04/aspnet-ajax-rendering-a-gmail-like-loading-indicator/
Posted by: Emad Ibrahim | August 4, 2007 07:30 PM
Hey Matt-
Thanks for the code. I have it almost working, but there's one kink: I want the UpdateProgress control to always show up in the center of my GridView, so I removed the if statement around the code that centers the control, and removed the else statements. Unfortunately, I'm not getting the results your sample shows: the first time the UpdateProgress control shows, it's aligned to the left edge of the GridView. The next load, it moves a bit to the right, and then on the third load, it finally centers.
I have a feeling it's related to the fact that I dynamically show one of two GridViews based on a user's choice in a radioButtonList. I altered your JavaScript code so that if the variable holding the first gridview is null, it then sets the variable to the second gridview; but I'm experiencing the side effects I mentioned above.
Any ideas?
Thanks,
-Andrew
Posted by: Andrew Cushen | September 16, 2007 03:48 PM
Great idea.
I will try it asap.
Posted by: ASP.NET applications | October 9, 2007 03:54 AM
hi
I want to use this in asp.net
ver1.1 can i do this?
Posted by: arup | November 22, 2007 04:13 AM
I think the idea is great, doing it in that way, because we can use it in every control of asp.net tech.
Posted by: Arturo | December 5, 2007 06:14 PM
when i use this
// get the gridview element
var gridView = $get('');
ERORR!
this is not declared whats this?
Posted by: waqar | December 14, 2007 05:14 PM
I think the idea is great, doing it in that way, because we can use it in every control of asp.net tech.
Posted by: chetuo | January 28, 2008 12:40 PM
Hi Matt,
Great Stuff! Very usable and simple to implement. Thanks a lot man.
Posted by: smile | March 24, 2008 10:52 PM
Does not work with 2 level deep nested UpdatePanels. Do you have any updates to your solution? Would love to have this working with nested UpdatePanels.
Thx
Posted by: Anonymous | April 7, 2008 06:29 PM
I found a problem when using this with a page that uses a ModalPopupExtender. After pressing the update button on the ModalPopupExtender update panel, the updateProgressDiv remains hidden underneath the modal popup. Is there some way to force the updateProgressDiv to always be on top?
Thanks,
Robert
Posted by: RobertC | April 21, 2008 10:56 AM
Thanks you a lot.
Posted by: Prefabrik | May 8, 2008 05:06 AM