I had a situation here at a client where I had a Page_Error handler which needed to perform some custom error handling (display an ajax modal dialog, and such) for unhandled exceptions that occur during an ajax async/partial postback.

The default behavior for unhandled errors that occur during an async postback, is for an alert box to be displayed with the error information. This might be alright for most micky mouse apps – but for production systems, you are probably not going to get the customers approval.

So what do you do?
Well, lucky for me – I have a mate named Glav, the god of all things ASP.NET and Ajax.

He showed me the way around this, by disabling the default behavior, by telling ajax that we have handled the error ourselves.

Here’s the code – if you register this script, the popup should go away.


// handle the end of the ajax page request
function EndRequestHandler(sender, args)
{
// if we have an error
if(args.get_error() != 'undefined')
{
// extract the error for giggles
//var errorMessage = args.get_error().message; // here's the message
// tell the framework that we have handled the error appropriately (let page_error handle it on the server)
args.set_errorHandled(true);
}
}

// Ajax calls this function on page load
function pageLoad(sender, e)
{
// hook the event to kill the error
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}

And away you go!
Basically, it registers for the EndRequest event, then tells ajax that we have handled the error ourselves by calling set_errorHandled(true); Neat!

Thanks again Paul!

Advertisements

About cosier

Matthew Cosier is the Chief Technology Officer at Hazaa. We are a group of experienced consultants based in Melbourne, Australia who solve business problems using Microsoft Technology. For more information, please visit http://www.hazaa.com.au

One response »

  1. Paul Glavich says:

    Absolute pleasure Matty. Anytime mate.

    Thanks for the plug.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s