Tuesday, June 7, 2011

Execute code after Asynchronous postbacks

When we use ASP.NET UpdatePanel in our application and do postbacks, it results asynchronous postbacks from controls inside UpdatePanel which don’t trigger window.load() or jQuery’s $(document).ready() methods. To run a method after asynchronous postback, we need to use Sys.WebForms.PageRequestManager. It has add_endRequest() method which allows to attach method to this event which will run after asynchronous postbacks.
To do this, add these lines in your javascript code.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(loadText);

function loadText() {

// Add your code which will run after Asynchronous postback.

}

If you are doing something on window.load() or $(document).ready() event and want to execute it after postbacks from controls residing inside UpdatePanel, copy that in your asynchronous postback event handler(s) as well.

For attaching a method with Page Load event, we can use

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(eventHandler);