Cancelling ASP.NET Ajax error handling (getting rid of that peski alert box during partial postback errors)

23 08 2007

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 requestfunction 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 loadfunction 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!


Actions

Information

One response

23 08 2007
Paul Glavich

Absolute pleasure Matty. Anytime mate.

Thanks for the plug.

Leave a comment