aspx <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>
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;
}
點擊Button1按鈕後,會在<div id="error"></div>中顯示Sorry, an error has occurred: Custom Error!,其中Custom Error就是在Button1_Click中自訂的錯誤資訊,然後隔了三秒鐘後錯誤資訊自動消失是setTimeout(function(){ $get("error").innerHTML = ""; }, 3000);的作用。