gpt4 book ai didi

updatepanel - 更新面板未显示 ASP.NET 3.5 中的错误

转载 作者:行者123 更新时间:2023-12-03 18:17:05 27 4
gpt4 key购买 nike

在 Visual Studio 2008 中,如果您创建一个新的“Ajax 1.0 Enabled ASP.NET 2.0 Web Application”并粘贴以下代码:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button runat="server" ID="foo" Text="click me" onclick="foo_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>

背后的代码
protected void foo_Click(object sender, EventArgs e) {
throw new Exception("hello");
}

然后单击按钮,您将看到一个 javascript 警报,上面写着“你好”。如果您创建一个 .NET 3.5 Web 应用程序并粘贴相同的代码,则不会再显示任何警报。我错过了什么?

最佳答案

如果您只想修复浏览器 javascript 错误并向用户显示异常消息,则只需将其添加到您的 Masterpage 表单声明之后的某处:

<!-- This script must be placed after the form declaration -->
<script type="text/javascript">
Sys.Application.add_load(AppLoad);

function AppLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
}

function EndRequest(sender, args) {
// Check to see if there's an error on this request.
if (args.get_error() != undefined) {

var msg = args.get_error().message.replace("Sys.WebForms.PageRequestManagerServerErrorException: ", "");

// Show the custom error.
// Here you can be creative and do whatever you want
// with the exception (i.e. call a modalpopup and show
// a nicer error window). I will simply use 'alert'
alert(msg);

// Let the framework know that the error is handled,
// so it doesn't throw the JavaScript alert.
args.set_errorHandled(true);
}
}
</script>

除非您想自定义消息,否则您不需要捕获 OnAsyncPostBackError。 Go to my blog post if you want more information about this.

关于updatepanel - 更新面板未显示 ASP.NET 3.5 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1364907/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com