Ajax, Asp.net

Asp.net Ajax Events

ASP.NET AJAX framework provides various page events that are raised during processing ajax requests.

Following table shows details of Events,

Event Name Description
beginRequest Event Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
endRequest Event Raised after an asynchronous postback is finished and control has been returned to the browser.
initializeRequest Event Raised during the initialization of the asynchronous postback.
pageLoaded Event Raised after all content on the page is refreshed as the result of either a synchronous or an asynchronous postback.
pageLoading Event Raised after the response from the server to an asynchronous postback is received but before any content on the page is updated.

Uses

  • beginRequest: can be used to call custom script to set a request header or to start an animation that notifies the user that the postback is being processed.
  • endRequest: can be used to provide a notification to users or to log errors.
  • initializeRequest: can be used to cancel a postback.
  • pageLoaded/ pageLoading: can be used to provide a custom transition effect for updated content.

How To Use

  • Register beginRequest event syntax,
Sys.WebForms.PageRequestManager.instance.add_beginRequest (beginRequestHandler)
  • Remove beginRequest event syntax,
Sys.WebForms.PageRequestManager.instance.remove_beginRequest (beginRequestHandler)

Example

Add following script into your page,


function pageLoad() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
 
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
prm.add_beginRequest(beginRequestHandler);
prm.add_pageLoaded(pageLoaded);
}
 
function InitializeRequest(sender, args) {
//TODO: write InitializeRequest code here
}
 
function BeginRequestHandler(sender, args) {
//TODO: write BeginRequestHandler code here
}
 
function EndRequest(sender, args) {
//TODO: write EndRequest code here
}
 
function pageLoaded(sender, args) {
 //TODO: write pageLoaded code here
}
 

Ajax, Asp.net, Javascripts

Cancel previous Ajax requests on new request

If you are using ajax in web application , then there might be possibility that user does repetitively action such as click on button more than once which sends same asynchronous requests for every single click,
Before completing first request, another request for the same functionality is get sent and so on…

Here is the simple solution that helps to cancels previous ajax request,

Copy following two java scripts functions on page. it will cancel previous asynchronous requests. It works in asp.net ajax.

  
function pageLoad()
        {
Sys.WebForms.PageRequestManager.getInstance( ).add_initializeRequest(cancelPostBack);
         }
function cancelPostBack(sender, args)
        {
if (Sys.WebForms.PageRequestManager.getInstance( ).get_isInAsyncPostBack())
            {
              args.set_cancel(true);
            }
        }