<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" AllowCustomErrorsRedirect="false" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<div id="error"></div>
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, e)
{
e.set_errorHandled(true);
$get("error").innerHTML = "Sorry, an error has occurred: " + e.get_error().message;
setTimeout(function(){ $get("error").innerHTML = ""; }, 3000);
});
</script>
</form>
<asp:ScriptManager ID="ScriptManager1" runat="server" AllowCustomErrorsRedirect="false" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<div id="error"></div>
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, e)
{
e.set_errorHandled(true);
$get("error").innerHTML = "Sorry, an error has occurred: " + e.get_error().message;
setTimeout(function(){ $get("error").innerHTML = ""; }, 3000);
});
</script>
</form>
cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
throw new Exception("Custom Error!");
}
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager.GetCurrent(this).AsyncPostBackErrorMessage = e.Exception.Message;
}
{
}
protected void Button1_Click(object sender, EventArgs e)
{
throw new Exception("Custom Error!");
}
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager.GetCurrent(this).AsyncPostBackErrorMessage = e.Exception.Message;
}
点击Button1按钮后,会在<div id="error"></div>中显示Sorry, an error has occurred: Custom Error!,其中Custom Error就是在Button1_Click中自定义的错误信息,然后隔了三秒钟后错误信息自动消失是setTimeout(function(){ $get("error").innerHTML = ""; }, 3000);的作用。