CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

Cancel Multiple Asynchronous Postback from Same Button in ASP.Net AJAX
Submitted By Satheesh Babu B
On 11/20/2008 5:43:32 AM
Tags: asp.net ajax,CodeDigest,javascript  

Cancel Multiple Asynchronous Postback from Same Button in ASP.Net AJAX

 

In ASP.Net AJAX applications, when the users initiated a bulk server processing there are chances that the users may click the button again and again which results in duplicate processing on the server. This is because, in classic ASP.Net applications, the page will be refreshed and it will be blank till the response is received from server which indicates that the server is still progressing with the request, where as in AJAX application it's not.In ASP.Net AJAX, when we have duplicate request sent to the server then the user will be provided with the response of the latest request.

One way to prevent this is to use the UpdateProgress control to notify the users that their request still progressing. Read my article on Using UpdateProgress Control Effectively. 

 

An alternate way is to prevent the users to click the button again and again by using the following script. The following script will alert the user with a message if he clicks the same button again when there is already asynchronous processing for the button is still progressing in the server.

 

<script language="JavaScript">

     var Page;

     function pageLoad()

    {      

       Page = Sys.WebForms.PageRequestManager.getInstance();  

       Page.add_initializeRequest(OnInitializeRequest);

    }

 function OnInitializeRequest(sender, args)

    {       

      var postBackElement = args.get_postBackElement();

      if(Page.get_isInAsyncPostBack() && postBackElement.id == "btnSave")

      {

        alert('Your Request is already in processing state! Pls be patient!!');

        args.set_cancel(true);

      }

    }     

  </script>

 

Through PageRequestManager class, we can hook in and find out if it is a duplicate postback and cancel the request in OnInitializeRequest() event.

btnSave is the button name that initiated the asynchronous request and we are canceling the current request by calling set_cancel(true) method.

 

Alternate way is to disable the button till the response is received. Read it here .

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..