Coping with Click-Happy AJAX Application Users

Users can be impatient while waiting for data to be returned to a page (I'll admit I'm guilty of this occasionally).  Fortunately, ASP.NET AJAX makes it easy to handle cases where impatient users continually click a refresh button (or other type of button) in an ASP.NET AJAX page causing extra load to be placed on your server.

Instead of allowing continuous requests to reach the server before their initial request has returned, you can easily let users know to kindly wait until their first request has returned and then cancel the subsequent request they've made.  This can be done by using the PageRequestManager to handle the initializeRequest event.  An example of performing this task is shown below.  More details can be found in my latest .NET Insight article.

Sys.Application.add_init(Init);
var 
prm = null;

function 
Init(sender)
{
   prm 
Sys.WebForms.PageRequestManager.getInstance();
   if 
(prm)
   {
      
if (!prm.get_isInAsyncPostBack())
      {
          prm.add_initializeRequest(InitRequest)
;
      
}
   }
}

function InitRequest(sender,args)
{
    
if (prm.get_isInAsyncPostBack() && 
      args.get_postBackElement().id 
== 'btnRefresh') {
       args.set_cancel(
true);
       if 
($get("divChill").style.visibility !"visible")
       {
          $
get("divChill").style.visibility "visible";
          
setTimeout("ClearDiv()"2000);
       
}
    }
}

function ClearDiv()
{
    $
get("divChill").style.visibility "hidden";
}

comments powered by Disqus

1 Comment

  • Good stuff....thanks for commenting Dave. For anyone else who may be interested, here's the direct link to the post Dave mentions. There's quite a bit of AJAX-specific content on the blog.

    http://encosia.com/index.php/2007/01/16/css-style-as-ajax-progress-indicator/

Comments have been disabled for this content.