【问题标题】:UpdatePanel Exception HandlingUpdatePanel 异常处理
【发布时间】:2010-12-22 00:17:29
【问题描述】:

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

如何优雅地处理 UpdatePanels 中发生的错误以提供无缝且无 Javascript 错误的实现?

【问题讨论】:

    标签: asp.net exception asp.net-ajax error-handling updatepanel


    【解决方案1】:

    如果您只想修复浏览器 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.

    【讨论】:

    • 成功了!非常感谢!
    【解决方案2】:

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

    这里有一些资源可以帮助你:

    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>
    

    【讨论】:

    【解决方案3】:

    您能否覆盖页面级错误方法,捕获异常并处理您认为合适的方式。

    protected override void OnError(EventArgs e)
    {
        //show error message here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多