Cancelling an AsyncPostback

And how do you hook up the button in my previous post to give the user the option to cancel a long-running operation? You simply need to call a JavaScript function (CancelAsyncPostBack() in my case) as follows:

   <script language="JavaScript" type="text/javascript">

  var prm = Sys.WebForms.PageRequestManager.getInstance();

  function CancelAsyncPostBack()
  {
    if (prm.get_isInAsyncPostBack())
    {
      prm.abortPostBack();
    }
  }
  
  </script>

Before calling the function you have to hook up to the PageRequestManager instance for the current page (the PageRequestManager object is responsible for managing partial page updates for UpdatePanel controls on the page). Calling Sys.WebForms.PageRequestManager.getInstance() gets you the current instance. PageRequestManager is used by CancelAsyncPostBack() to [a] check if an AsyncPostBack is currently in progress (when the cancel request is received) and [b] abort the AsyncPostBack by calling the abortPostback() method.

Technorati tags: asp.net, ajax