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

4
在 Visual Studio 2008 中,如果您创建一个新的“启用 Ajax 1.0 的 ASP.NET 2.0 Web 应用程序”,并粘贴以下代码:
<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");
        }

然后点击按钮,你会看到一个显示“hello”的JavaScript警告框。如果在创建.NET 3.5 Web应用程序并粘贴相同的代码后,不再显示任何警告框,请问我错过了什么?


那么...没有人在使用.NET 3.5,或者没有人担心会出现错误吗? :) - aquinas
2个回答

15
如果您只想修复浏览器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异常。如果您需要更多信息,请访问我的博客文章


6

在ASP.NET AJAX扩展1.0和ASP.NET AJAX 3.5之间的默认行为明显有所改变。这可以通过查看Sys.WebForms.PageRequestManager的默认endPostBack事件处理程序来看出。前者使用Alert显示错误,而后者只是重新抛出错误。

// ASP.NET AJAX Extensions 1.0
function Sys$WebForms$PageRequestManager$_endPostBack(error, response) {
  this._processingRequest = false;

  this._request = null;
  this._additionalInput = null;

  var handler = this._get_eventHandlerList().getHandler("endRequest");
  var errorHandled = false;
  if (handler) {
    var eventArgs = new Sys.WebForms.EndRequestEventArgs(error, this._dataItems, response);
    handler(this, eventArgs);
    errorHandled = eventArgs.get_errorHandled();
  }
  this._dataItems = null;
  if (error && !errorHandled) {
    alert(error.message);
  }
}


// ASP.NET 3.5
function Sys$WebForms$PageRequestManager$_endPostBack(error, executor, data) {
  if (this._request === executor.get_webRequest()) {
    this._processingRequest = false;
    this._additionalInput = null;
    this._request = null;
  }
  var handler = this._get_eventHandlerList().getHandler("endRequest");
  var errorHandled = false;
  if (handler) {
    var eventArgs = new Sys.WebForms.EndRequestEventArgs(error, data ? data.dataItems : {}, executor);
    handler(this, eventArgs);
    errorHandled = eventArgs.get_errorHandled();
  }
  if (error && !errorHandled) {
    throw error;
  }
}

如果您想在ASP.NET AJAX 3.5代码中显示警报,只需要进行一些小的更改。
首先,您需要为ScriptManager的AsyncPostBackError事件添加处理程序,然后设置AsyncPostBackErrorMessage。
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
    ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message;
}

接下来需要为客户端的PageRequestManager的endRequest事件添加处理程序。在那里,您可以获取服务器端设置的AsyncPostBackErrorMessage,并使用Alert将消息显示给用户。

function pageLoad()
{
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);  
}

function onEndRequest(sender, args)
{
  var msg = args.get_error().message;
  alert(msg);
  args.set_errorHandled(true);
}

我希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接