在服务器代码中自定义错误处理
通过设置 AsyncPostBackErrorMessage 属性和处理 ScriptManager 控件的 AsyncPostBackError 事件来自定义错误处理。
使用客户端脚本以自定义错误处理
通过处理 PageRequestManager 类的 endRequest 事件来自定义错误处理。
示例代码
ASPX
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxTest._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- <style type="text/css">
- #UpdatePanel1 {
- width: 200px; height: 120px;
- border: solid 1px gray;
- }
- #AlertDiv{
- left: 40%; top: 40%;
- position: absolute; width: 200px;
- padding: 12px;
- border: #000000 1px solid;
- background-color: white;
- text-align: left;
- visibility: hidden;
- z-index: 99;
- }
- #AlertButtons{
- position: absolute; right: 5%; bottom: 5%;
- }
- </style>
- </head>
- <body id="bodytag">
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server"
- onasyncpostbackerror="ScriptManager1_AsyncPostBackError" >
- </asp:ScriptManager>
- 自定义 ASP.NET UpdatePanel 控件的错误处理
- <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
- <ContentTemplate>
- <asp:CheckBox ID="CheckBox1" Text="Throw Exception" runat="server" />
- <br />
- <asp:Button ID="Button1" runat="server" Text="Send" οnclick="Button1_Click" />
- <br />
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- </div>
- <div id="AlertDiv">
- <div id="AlertMessage">
- </div>
- <br />
- <div id="AlertButtons">
- <input id="OKButton" type="button" value="OK" runat="server" οnclick="ClearErrorState()" />
- </div>
- </div>
- </form>
- </body>
- </html>
- <script type="text/javascript" language="javascript">
- var divElem = 'AlertDiv';
- var messageElem = 'AlertMessage';
- var bodyTag = 'bodytag';
- Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
- function ToggleAlertDiv(visString)
- {
- if (visString == 'hidden')
- {
- $get(bodyTag).style.backgroundColor = 'white';
- }
- else
- {
- $get(bodyTag).style.backgroundColor = 'gray';
- }
- var adiv = $get(divElem);
- adiv.style.visibility = visString;
- }
- function ClearErrorState() {
- $get(messageElem).innerHTML = '';
- ToggleAlertDiv('hidden');
- }
- function EndRequestHandler(sender, args)
- {
- if (args.get_error() != undefined)
- {
- var errorMessage;
- if (args.get_response().get_statusCode() == '200')
- {
- errorMessage = args.get_error().message;
- }
- else
- {
- // Error occurred somewhere other than the server page.
- errorMessage = 'An unspecified error occurred. ';
- }
- args.set_errorHandled(true);
- ToggleAlertDiv('visible');
- $get(messageElem).innerHTML = errorMessage;
- }
- }
- </script>
CS
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- namespace AjaxTest
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(Button1);
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Text = DateTime.Now.ToString();
- if (CheckBox1.Checked)
- {
- throw new ArgumentException("aa");
- }
- }
- /// <summary>
- /// 服务端处理异常事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
- {
- // e.Exception
- ScriptManager1.AsyncPostBackErrorMessage = "You Handle Error";
- }
- }
- }