Browser not handling exception from AJAX panel, ASP.NET c#
i am having trouble catching errors in an AJAX panel. Even when i throw an exception in the c# code behind the front en开发者_运维技巧d completely ignores it.
Here is the code i have setup, can anyone see why?
I ideally want to show a js alert window on error.
Code Behind:
protected void btnX_Click(object sender, EventArgs e)
{
throw new ApplicationException("test");
}
protected void ScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager.AsyncPostBackErrorMessage = e.Exception.Message;
}
Markup:
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, e)
{
window.alert(e.get_error().name);
}
</script>
<asp:ScriptManager ID="ScriptManager" runat="server" AllowCustomErrorsRedirect="true" OnAsyncPostBackError="ScriptManager_AsyncPostBackError" />
I think you should be displaying the 'message' error property and not 'name'
see http://msdn.microsoft.com/en-us/library/bb383810.aspx
function EndRequestHandler(sender, e)
{
if (e.get_error() != undefined)
{
var errorMessage = e.get_error().message;
e.set_errorHandled(true);
alert(errorMessage)
}
}
精彩评论