Ajax Error information with stack trace ASP.Net Ajax is great for developing web applications. However there are some issues in presenting server side exceptions in the browser nicely. I actually needed the entire stack trace to be shown in the screen. Whenever I had an error while using Ajax, I used to get an alert with error message. Sometimes even I got notification in the status bar of the browser window. Instead of getting the error on the alert box, I wanted to get it on the body or label in the body of the browser window. Ajax Error Handling support is already available, but using them is a bit not user friendly.
JavaScript to show error messages
Since the exception is passed to the browser from server side, we need a lot of Ajax Error Handling to do in the JavaScript. There are two events needs to be registered Begin Request and End Request in the Page Load event. PageLoad Event can be registered using Sys.Application.add_load (PageLoad) event. We need a label or DIV to display the error messages.
Register Events on PageLoad for Ajax Error Handling
Two events needs to be registered BeginRequest, and EndRequest in the Page Load event using add_beginRequest and add_endRequest respectively
BeginRequest Event
Here we need to make the label invisible, because if there is no error no needs to display this label. Basically using a style to make it invisible will be sufficient.
EndRequest Event
This is the main event to deal with the exceptions passed to the client. Here the invisible label will become visible. And the passed error description can be fetched using args.get_error ().description.
Ajax Application Generator
Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
To display Stack Trace
With the above changes we can display the latest error message. To get the stack trace of the error from the server, we need to use the ScriptManager’s AsyncPostBackError event. The second argument contains the exception, so accessing and passing the stack trace is simpler. The modified error message can be passed to the client by assigning ScriptManager1.AsyncPostBackErrorMessage.
Finally you will be able to see the stack trace, but the information is not presented in a well readable format. The error message is not meant for the web, so the line breaks have to be replaced by
Read more with complete working source code (Ajax Error Handling)
Sunday, April 4, 2010
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment