在我正在构建的ASP.NET Web应用程序中实现的UpdatePanel中出现异常时,它们会在页面上导致JavaScript错误,并在警报中提供一些高级错误输出。这对于开发来说是好的,但是一旦系统处于生产阶段,由于多种原因显然是不合适的。我可以围绕Try Catch等麻烦的活动来控制Javascript错误,但在某些情况下,我想在主页面上采取措施来支持用户体验。UpdatePanel异常处理
我该如何处理UpdatePanels中出现的错误,以提供无缝和无错误的Javascript实现?
在我正在构建的ASP.NET Web应用程序中实现的UpdatePanel中出现异常时,它们会在页面上导致JavaScript错误,并在警报中提供一些高级错误输出。这对于开发来说是好的,但是一旦系统处于生产阶段,由于多种原因显然是不合适的。我可以围绕Try Catch等麻烦的活动来控制Javascript错误,但在某些情况下,我想在主页面上采取措施来支持用户体验。UpdatePanel异常处理
我该如何处理UpdatePanels中出现的错误,以提供无缝和无错误的Javascript实现?
您可以使用上的ScriptManager(服务器端),并在PageRequestManager(客户端)的EndRequest事件中使用的UpdatePanel时完全处理服务器端错误AsyncPostBackError事件的组合。
这里有一些资源可以帮助你:
Customizing Error Handling for ASP.NET UpdatePanel Controls
Error Handling Customization for ASP.NET UpdatePanel
这里有一个简单的例子:
// Server-side
protected void ScriptManager1_AsyncPostBackError(object sender,
AsyncPostBackErrorEventArgs e) {
ScriptManager1.AsyncPostBackErrorMessage =
"An error occurred during the request: " +
e.Exception.Message;
}
// Client-side
<script type="text/javascript">
function pageLoad() {
Sys.WebForms.PageRequestManager.getInstance().
add_endRequest(onEndRequest);
}
function onEndRequest(sender, args) {
var lbl = document.getElementById("Label1");
lbl.innerHTML = args.get_error().message;
args.set_errorHandled(true);
}
</script>
您可以重写页面级错误方法,捕获异常并处理您认为合适的方式。
protected override void OnError(EventArgs e)
{
//show error message here
}
我这里写关于一个简单的方法吗这个: http://www.wagnerdanda.me/2010/01/asp-net-ajax-updatepanel-error-exception-handling-the-simple-way/
如果你只是想修复浏览器的JavaScript错误,并显示异常信息给用户,你只需要在窗体声明后的某个地方本添加到您的母版:
<!-- 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.
工作!非常感激! – 2013-03-15 19:34:54
谢谢你,非常感谢。 – Chris 2009-12-01 20:05:28
在我修复了另一个相关问题后,这很好用:https://stackoverflow.com/questions/1671881/scriptmanager1-asyncpostbackerrormessage-not-showing-error-message – madamission 2018-02-19 00:37:24