CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Error Handling in ASP.Net Ajax Applications

By Satheesh babu
Posted On Aug 20,2008
Article Rating:
Average Rating: 5
No of Ratings: 1
No of Comments: 3
Category: ASP.Net AJAX
Print this article.

Error Handling in ASP.Net Ajax Applications

 

My previous articles, Implementing AJAX Enabled Applications in ASP.Net and Building AJAX Applications using ASP.Net AJAX provided a good understanding on AJAX and implementing AJAX enabled application using ASP.Net AJAX. Moving forward, we will concentrate on implementing AJAX applications with more advanced features. This article will discuss on various error handling techniques and the ways of notifying errors to the users available 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

<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError" />

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.

 

  <script type="text/JavaScript" language="JavaScript">

    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);

    }    </script>

<div id="divError" style="color:Red" ></div>




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

 

<customErrors> section in the web.config

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

    <customErrors defaultRedirect="Error.aspx" mode="On">

    </customErrors>

 

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.

 

Reference

Custom Error Page in ASP.Net 2.0

 

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. We will see more about implementing AJAX applications in coming days. Stay Tuned!!

Happy Coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
GOOD ARTICLE
Nice explanation.
Ajax Error
it is simply a great article. I have been looking for managing this error but only your post made my doubts clear
thanks
How To Get Stack Trace
I have done a lot of error handling techniques for Ajax. Only Problem i am having is I can't able to get Stack Trace when Using Client side technique to show the error mesage and its stack trace. If you have any idea then please reply for this post http://forums.asp.net/p/1287242/2482288.aspx Thanks