Canceling Async PostBack

Asp.net Ajax can perform at most one Async PostBack at a time. If the user initiates a second PostBack, it automatically aborts the previous request and initiates a new asynchronous PostBack. If this is not the behavior you desire, you have the ability to cancel new request until the existing request completes. In order to cancel new request you will have to register with beginRequest event of the pagerequest manager and check to see if there is an existing request that has not completed. If it is not completed than you can cancel the new request. The reason asp.net Ajax has limitations for only 1 Async PostBack is because of viewstate. If it were to allow more than 1 request than viewstate would get out of sync. If the current request takes too long to come back you can also have a cancel button on the UI and let the user cancel the existing page request. Here is the markup that I have written that displays the behavior.

image

In the above code, I am registering for initializeRequest event inside of pageload event. InitializeRequest event is raised before an asynchronous request is sent to the server. Basically I am checking with the pagerequest manager if there is an existing request that is still waiting to be arrived. If get_isInAsyncPostBack returns true than we go ahead and cancel the new request. I also have a cancel button that simply tells the pagerequest manager to cancel existing Async request. Canceling the Async request simply tells the page request manager to ignore the response received from the server. It will not prevent the server from processing the entire request.

No Comments