在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应用程序并粘贴相同的代码,则不再显示任何警报。我错过了什么?
答案 0 :(得分:15)
如果您只想修复浏览器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.
答案 1 :(得分:6)
ASP.NET AJAX Extensions 1.0和ASP.NET AJAX 3.5中的默认行为肯定会发生变化。通过查看Sys.WebForms.PageRequestManager的默认endPostBack事件处理程序可以看到这一点。前一版本使用警报显示错误,而后一版本仅重新显示错误。
// 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);
}
我希望这会有所帮助。