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())

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: ,

Comments (4)
  1. Gopinath says:


    If u use any session variables on the aspx page, on the start MS AJAX wont even generate an Async call from client [all are sync calls !] so the call to this method prm.get_isInAsyncPostBack() always returns false,

    over which u cannot cancel even this sync call either until the currnet call completes !

    here is my post :



  2. MikeO [MSFT] says:

    Hi Gopi. That’s not the behaviour I’m seeing – it seems to work fine on my page. I tested on the tutorial page you point to and I do see the behaviour. But on my page I have a thread sleep for 10s and I can still cancel the request and submit another – at least it gives the illusion of working. As Steve points out in his reply to your forum posting, there’s no way to cancel the server reqeust so a subsequent request has to wait for the initial one to complete. And I definitely see prm.get_isInAsyncPostBack() = true on my initial request. My page behaves exaclty as I would expect so I’m not sure what the difference is.

  3. Gopinath says:

    Mike, just add a [or on the tutorial sample]

    Session[“Test”] = “123”; on your page_load and try using that

    Session in your call back method, along with a thread.sleep for 30 sec.

    Now cancel the async postback using cancel update, it wont cancel the update until the full 10 sec is completed on server and the response returns back to client !

    The main point is when u use any kind of Session Variables on your server code, u cannot cancel the async request !



  4. MikeO [MSFT] says:

    Hi Gopi. That’s exactly what I did. I see the same behaviour as you describe on the tutorial sample. But on my page it behaves differently so the user experience is much better. My "busy" dialogue goes away immediately. You’re right that the server wont process the request until the previous request is completed but from a user perspective it just appears that the 2nd request takes a little longer than normal. I certainly don’t see the behaviour you described earlier where you say that you can’t generate an async call so prm.get_isInAsyncPostBack always returns true. Try it for yourself by just creating a simple page with an UpdatePanel and an UpdateProgress control. Add an HTML button to the UpdateProgress control and hook that to the JavaScript cancel async postback function. I would publish the page on my site but my hoster doesn’t have ASP.NET AJAX available yet (as far as I know). Mike

Comments are closed.

Skip to main content