Error Handling in ASP.Net Ajax Applications

Error Handling in Asp.Net Ajax Applications

By default, whenever there are any error occurred in the server side processing when using ASP.Net Ajax, the error message is notified to the user using a message box (alert). The error message that is displayed on the message box will be the actual error message contained in the original exception’s Message property. In web applications, we normally show an error page with a generic message to the user and log the technical error information to a log file or database or email the error message. This article will help us to customize the errors and notify the users in a better way that gives a better user experience. Moving forward, we will see,

  • Show a Generic Error Message.
  • Display Generic Error with Technical information.
  • Displaying error message on the page instead of MessageBox.
  • Using ASP.Net Error Handling Techniques.
Show a Generic Error Message

It will be better, if we suppress the technical error message and instead show a generic error message to the user when there is an error occurred in the server side processing. To do this, ScriptManager object has a property called AsyncPostBackErrorMessage which can be used to specify a generic error message.

For example,
ScriptManager1. AsyncPostBackErrorMessage = “Error Occurred”;

This will always give “Error Occurred!!” message for all the exceptions happened in server side processing. Refer the below figure.

Display Generic Error with Technical Information

There is an event called AsyncPostBackError which will be called on the server when there is an error occurred. We can set AsyncPostBackErrorMessage property of ScriptManager object either declaratively or can be customized in this event. e.Exception.Message property will give the actual technical error information which can be logged or displayed to the user.

ASPX

Codebehind
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager1.AsyncPostBackErrorMessage = “Error Occured!!n” + e.Exception.Message;
}

Refer the below figure for better understanding.

Displaying the error message on the page instead of MessageBox

Every time when there is an error, it is then notified to the users through a Message box in ASP.Net AJAX application. It will be better if we suppress this message box and display the error information in the page instead of the message box.
To do this, we can utilize the endRequest() event of PageRequestManager object which manages the AJAX in the client side. We can suppress the message box and display the actual error in the page through this event. To get an instance of PageRequestManager, we can call the method Sys.WebForms.PageRequestManager.getInstance().
To display the error message in the page instead of a message box we can use the following script.

function pageLoad()
{
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_endRequest(endRequest);
}
function endRequest(sender, args)
{
var Error = args.get_error();
document.getElementById(“divError”).innerHTML = Error.message;
args.set_errorHandled(true);
}

 

There is one PageRequestManager object per page which manages partial-page rendering in the browser. PageRequestManager class have series of events which gets executed for an Asynchronous postback.
The order of the events are,
initializeRequest
beginRequest
pageLoading
pageLoaded
endRequest

In the above code, we are displaying the error message in a DIV tag by suppressing the message box in endRequest() event. The EndRequestEventArgs(args, 2nd argument of endRequest event) object have 2 properties called Error and ErrorHandled. Calling args.set_errorHandled(true) sets the ErrorHandled property to true and thus, it suppresses the message box. Refer the below figure where the error message is displayed in the page.

Using ASP.Net Error Handling Techniques

section in the web.config
The custom error section in Web.Config setting will still work with ASP.Net AJAX applications. For example,

The above setting in web.config will redirect the user to the error page whenever any error occurs in the server processing. There is a property called AllowCustomErrorsRedirect in ScriptManager object which is set to true by default. Whenever we have this property enabled and endRequest() event is also implemented in Javascipt for error handling, the custom error page will precede and the user will be redirected to Error page. So, we have to make the AllowCustomErrorsRedirect property to false to display the error in the same page. The same holds true when we use error handling through Application_Error event in Global.asax and Page_Error Event in page level. Refer my article about Custom Error Handling on ASP.Net in reference section.

Conclusion
Thus, we have understood how to handle errors and notify the users in ASP.Net AJAX applications. In real world applications, user should be notified with a generic error instead of providing him more information technically which the default behaviour. With this article, we can now suppress this behaviour and can customize it according to our application needs.

You may also like...

2 Responses

  1. Ravi says:

    Good One..

  2. Anonymous says:

    Nice Article…

Leave a Reply

Your email address will not be published. Required fields are marked *