使用 ScriptManager 处理异步错误消息
这件事让我发疯。我试图在部分回发期间拦截服务器端的异常,设置一条错误消息以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了在服务器端处理逻辑之外,您还必须添加脚本才能处理客户端的错误。
该脚本正在处理 end_Request 事件,如果出现错误,则会发出警报。
In addition to handling logic on server side you have to add script in order to handle error on client side.
This script is handling end_Request event, and if there was some error fires alert.