使用 ScriptManager 处理异步错误消息

发布于 2024-11-28 06:16:13 字数 1924 浏览 1 评论 0原文

这件事让我发疯。我试图在部分回发期间拦截服务器端的异常,设置一条错误消息以 JavaScript 警报的形式显示在客户端上。

但我只是在 Firefox 控制台上收到以下错误:

uncaught exception: [Exception... "'Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Tentativo di divisione per zero.' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "JS frame :: resource://firebug_rjs/net/spy.js :: callPageHandler :: line 796" data: no]

并且根本没有警报。

我在一个涉及 Telerik 组件的复杂项目中遇到了这个问题,但我已将问题简化为其基本原理,按照这个简单的例子,问题仍然存在。

案例:

Default.aspx

<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" OnAsyncPostBackError="scriptManager_OnAsyncPostBackError"></asp:ScriptManager>
<div>
    <asp:UpdatePanel ID="upd" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_OnClick" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

Default.aspx.cs

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

protected void btnSend_OnClick(object sender, EventArgs e) 
{
    // this will throw and Exception
    int aa = Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text);
}

我已经尝试过使用 VS2010/.NET 4.0 和 VS2008/.NET 3.5,在 Firefox 和 Internet Explorer 中,问题是一样的。

请问有人有什么想法吗?

This thing is drive me crazy. I'm trying to intercept an exception server-side, during a partial postback, setting an error message to show on the client in a javascript alert.

But I just receive the following error on the firefox console:

uncaught exception: [Exception... "'Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Tentativo di divisione per zero.' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "JS frame :: resource://firebug_rjs/net/spy.js :: callPageHandler :: line 796" data: no]

and no alert at all.

I got this problem on a complex project that involves Telerik components, but I've reduced the problem to its fundamentals, following this simple example, and the problem still exists.

The case:

Default.aspx

<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" OnAsyncPostBackError="scriptManager_OnAsyncPostBackError"></asp:ScriptManager>
<div>
    <asp:UpdatePanel ID="upd" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_OnClick" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
</form>

Default.aspx.cs

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

protected void btnSend_OnClick(object sender, EventArgs e) 
{
    // this will throw and Exception
    int aa = Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text);
}

I've tried with VS2010/.NET 4.0 and with VS2008/.NET 3.5, in Firefox and Internet Explorer, the problem is the same.

Please, anyone has some idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裸钻 2024-12-05 06:16:13

除了在服务器端处理逻辑之外,您还必须添加脚本才能处理客户端的错误。

 <script>
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            if (args.get_error() != undefined) {
                alert(args.get_error());
            }
        }
    </script>

该脚本正在处理 end_Request 事件,如果出现错误,则会发出警报。

In addition to handling logic on server side you have to add script in order to handle error on client side.

 <script>
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            if (args.get_error() != undefined) {
                alert(args.get_error());
            }
        }
    </script>

This script is handling end_Request event, and if there was some error fires alert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文