UpdatePanel异常处理 [英] UpdatePanel Exception Handling

查看:182
本文介绍了UpdatePanel异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在构建的ASP.NET Web应用程序中实现的UpdatePanels中出现异常,它们在页面上导致JavaScript错误,并在警报中提供了一些高级错误输出。这是发展的好处,但一旦系统在生产中,显然没有好处有多个原因。我可以通过Try Catch等来围绕麻烦的活动来控制Javascript的错误,但在某些情况下,我想在主页面等上采取行动来支持用户体验。

Where exceptions occur in the UpdatePanels I've implemented in the ASP.NET web app I'm building, they cause a JavaScript error on the page with some high level error output available in the alert. This is OKish for development, but as soon as the system is in Production, it's obviously no good for multiple reasons. I can surround troublesome activities with Try Catch etc to control the Javascript error but in some cases, I want to take actions on the main page etc to support the user experience.

如何正确地处理UpdatePanels中发生的错误以提供无缝和Javascript无错误的实现?

How do I go about handling errors that occur in UpdatePanels gracefully to provide a seamless and Javascript error free implementation?

推荐答案

您可以使用ScriptManager(服务器端)上的AsyncPostBackError事件和PageRequestManager(客户端)上的EndRequest事件的组合,以在使用UpdatePanel时完全处理服务器端错误。

You can use a combination of the AsyncPostBackError event on the ScriptManager (server-side) and the EndRequest event on the PageRequestManager (client-side) to fully handle server-side errors when using the UpdatePanel.

这里有几个资源可以帮助您:

Here are a couple resources that should help you:

自定义ASP.NET UpdatePanel控件的错误处理

ASP.NET UpdatePan的错误处理自定义el

这是一个简单的例子:

// 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>

这篇关于UpdatePanel异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆