ScriptManager1.AsyncPostBackErrorMessage not showing error message

3.6k views Asked by At

I've always used the following two bits of code (which use to work) to catch Ajax asyncPostBackErrors.

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

and

protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e) 
{ 
ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message; 
}

But now even though the unhandled exception has been caught in this event handler function and the AsyncPostBackErrorMessage been set with the Exception message, I'm always getting the same error reporting in the page in a alert box no matter what the exception message was, saying:

Error: Sys.WebForms.PageRequestManagerParserErrorException: The message recieved from the server could not be parsed. Common causes for this error are when the response is modified by calls to the Respnse.Write() ....

The error is the same error you would get if you had an unhandled asyncPostBack exception and you didn't wire up the Scriptmanger's asyncPostBackError event handler method.

No matter what I do I get the same error. What would be causing this?

2

There are 2 answers

0
vagfot On

It seems i'm facing the same problem after upgrading from VS 2005 to VS 2008. I use this code:

if (e.Exception is LandingPageUIExceptionInvalidMSISDN)
{
    msgresolved = "ErrorPopUpInvalidNumber";
    ScriptManager.GetCurrent(Page).AsyncPostBackErrorMessage = msgresolved;
}

in order to handle the exception and display a proper error message. The AsyncPostBackErrorMessage is assigned properly but at client side when the following is executed in JavaScript:

var ErrorPopUpDivID = e.get_error().message;
alert(ErrorPopUpDivID);

The ErrorPopUpDivID is not what i had asigned to AsyncPostBackErrorMessage. It used to work for me too. I'll try to figure out if it might be a .NET 3.5 issue.

Finally the following 3 lines of JavaScript solved my problem:

var ErrorPopUpDivID = e.get_error().message;
var re = new RegExp("Sys.*: ", "g");
ErrorPopUpDivID = ErrorPopUpDivID.replace(re, "");
2
jcj80 On

This may be a shot in the dark, but maybe global exception handling is taking place after your ScriptManager1_AsyncPostBackError function is called. If that exception handling is doing a server.transfer to an error page, then the HTML sent back will not be parsable by the script manager.

This was happening on one of my sites and was solved by adding Server.ClearError() to the ScriptManager1_AsyncPostBackError function. This prevented Global.Application_Error function from being called.