Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling in UpdatePanel with <CustomErrors mode="On" />

I have custom errors enabled in my web.config file:

<customErrors mode="On" defaultRedirect="~/ErrorPage.aspx" />

When an error happens during an asynchronous postback in an UpdatePanel, the response comes back as a code 200 and the content set as the error page.

This breaks when the UpdatePanel tries to parse the response and throws a JavaScript exception:

Sys.WebForms.PageRequestManagerParserErrorException: message received from the server could not be parsed.

Is there a way to properly handle this on the client side without having to disable the custom error, as I do not want to divulge any exception details?

like image 945
Xavier Poinas Avatar asked May 10 '11 02:05

Xavier Poinas


People also ask

What is customErrors mode?

CustomErrors supports the following modes: On – If defaultRedirect is specified, they will see that content. Otherwise, the default error screen with fewer details. Off – Detailed error details will be shown to the user. (the “yellow screen of death screen”)

How do I turn off custom error handling in IIS for my web site?

Click the Custom Errors tab. Select Off for custom error mode. Or navigate to the folder containing your application and open the web. config file in a text editor and edit by hand, and change the custom errors tag to <customErrors mode="Off" />.

How is an error handled by default during an asynchronous postback?

With asynchronous postbacks and partial rendering, an exception can go back to the script that initiated the callback, and the default behavior is to show the user the error message. This happens even if you have custom errors enabled for your application.


1 Answers

One way to handle this would be to wire up the OnAsyncPostBackError event on your ScriptManager control:

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

This page on MSDN has a full example using this method to handle async postback errors.

EDIT
To get around the issue with the custom error pages be sure to set the AllowCustomErrorsRedirect property on the ScriptManager to false. Once I set this property to false I was able to get the MSDN sample to work properly even though I had CustomErrors mode="On" set in the web.config.

like image 191
rsbarro Avatar answered Oct 21 '22 01:10

rsbarro