开发者

Browser not handling exception from AJAX panel, ASP.NET c#

开发者 https://www.devze.com 2023-01-01 00:06 出处:网络
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.

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)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消