This article is a continuation of the previous article [Ajax Client Page Life Cycle]. This article explains Event Arguments and their usage during asynchronous partial page loads.
The Page Life Cycle Event Handler Arguments
The Event Arguments are themselves an instance of the object respective to their event handlers. Each argument gives reference to other objects, methods and properties which enables us to handle these events in more detail.
The object of this class is used as argument by
intializeRequest event of
PageRequestManager class. The methods of this object are listed here, where
args is the instance of the
intializeRequestEventArgs class. The code snippet is also attached below each property for better understanding.
var ele = args.get_postBackElement();
args.get_postBackElement().disabled = true;
args.get_postBackElement().value = "Page is getting Loaded...";
The object of the class is used by
BeginRequest event. This object is more similar to the previous one. The object will be instantiated just before the request is sent to the server. The properties and code snippet are listed below:
The object is used by
pageLoading event. This gives reference for the two collections of update panels used in asynchronous postback.
- Panels deleted during the post, and
- Panels going to be updated
We can get the content of the update panels which are not yet updated here. These panels will get updated after the
pageLoaded event. The content of the update panel can be changed here, but it will not be reflected in the page when the cycle is finished, because the
pageLoaded event will place the new contents in the page. That will be visible to the user at the end.
var content = args.get_panelsUpdating().innerText;
pageLoaded event gives reference to this object. The
pageLoaded are more similar. Both events will be triggered after Server Page
Render event, but the
pageLoading event will be triggered before the content gets updated and
pageLoaded after content update. The methods are listed below:
var content = args.get_panelsUpdated().innerText;
EndRequest event, this is the last event in the asnychronous partial post back. This event will be raised after the postback finishes. The response is a member of this object. The special thing about this object is that it enables us to handle errors. The error object will also be a member of this object, if any error occurs during postback otherwise it is
null. We can also handle the error and set our own custom values to the error object that will pop up when an error occurs. As we have provision to handle the error, the error can also be suppressed, so that it will not be displayed to user also. This is done by the
errorHandled property. If it is set to
true, then errors are suppressed. The methods are listed below:
function MyEndRequest(sender, args)
if (args.get_error() != null)
The exception that happens during the asnychronous partial past back is discussed here. The exceptions are dynamically created by Ajax. The exceptions are listed below:
ParserErrorException happens when an error occurs in processing response. A situation for this is like using
Server.Transfer in the partial postback. In this scenario, the request is sent to the server and server transfers the request to some other page, where a client is not aware of this. So the expected page response fails as per the client, so it throws a error. The same situation can be avoided by
Response.Redirect, because it makes two round trips to the server, also the client is aware of the page redirection.
ServerErrorException happens for all the unhandled errors on the server side code. For example, trying to parse a
int, assigning value to non instanced object, or a similar kind of error raises this exception.
TimeoutException is as the name says. It is thrown when the response is not returned within the specific span of time. The
ScriptManager control takes care of these scenarios. We can avoid this by increasing the value of
AsyncPostBackTimeout property of
These errors can be caught in the
endRequest event. The
get_error() method can be used to retrieve the error object and the name property of the error object gives the full name of the error, and we can handle it accordingly. The below code snippet of
endRequest event explains this...
function MyEndRequest(sender, args)
var s = sender;
var a = args;
var msg = null;
if (a._error != null)
case "Sys.WebForms.PageRequestManagerServerErrorException" :
msg = "PageRequestManagerServerErrorException";
case "Sys.WebForms.PageRequestManagerParserErrorException" :
msg = "PageRequestManagerParserErrorException";
case "Sys.WebForms.PageRequestManagerTimeoutException" :
msg = "PageRequestManagerTimeoutException";
args._error.message = "My Custom Error Message " + msg;
The handlers can be removed the same way as we are adding it. The commented line below the add custom event handler is the code to remove the added custom event handlers. This note applies for the whole article. I have written it this way just for demonstration purposes.
_error.message is set with a custom value in the code snippet given above.
We still have a lot to discuss about Ajax. Let us look at those in future articles.
- 23rd May, 2008: Initial post