在做网站时,根据用户的提交常常会弹出如"添加成功","更新失败",也有很多时候有一些末预先写出的错误。
如 int.Parse(txtQuantity.Text),而浏览者输入一个50亿错成的错误……,各式各样的信息。
而且有很多信息常常要在 2N 个地方用到请问,像这种能不能(或如何)有效的组织起来,一起管理,不是每个页页都写一个 JavaScript.Alert("xxx"); 或 throw new Exception()
如 int.Parse(txtQuantity.Text),而浏览者输入一个50亿错成的错误……,各式各样的信息。
而且有很多信息常常要在 2N 个地方用到请问,像这种能不能(或如何)有效的组织起来,一起管理,不是每个页页都写一个 JavaScript.Alert("xxx"); 或 throw new Exception()
protected override void OnError(EventArgs e)
{
Exception ex = Page.Server.GetLastError();
//得到了异常 这里就可以做你想做的了
//比如以javascript弹出错误提示 Alert(ex.Message);
base.OnError(e);
}
Alert是自己定义的 如下 可以放在基类中 在其它子类中可随时调用
public static void Alert(string msg)
{
if (msg == null) return;
msg = msg.Replace("\\",OutChar.Slash).Replace("\"",OutChar.DBquote).Replace("'", OutChar.Quote);
((Page)HttpContext.Current.Handler).RegisterClientScriptBlock(HttpContext.Current.Timestamp.ToString(),"<script language=javascript>alert('"+msg+"');</script>");
}也可以在Global.ascx文件中的Application_Error方法中写处理过程
protected void Application_Error(Object sender, EventArgs e)
{
}也可以用.net的错误页指定
namespace Utility
{
public class MyHttpModule:IHttpModule
{
#region IHttpModule 成员 public void Dispose()
{
} public void Init(HttpApplication context)
{
context.Error += new EventHandler(this.Application_OnError);
} public void Application_OnError(Object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
{
context.Response.Write("<html><body style=\"font-size:14px;\">");
context.Response.Write("Error:<br />");
context.Response.Write("<textarea name=\"errormessage\" style=\"width:80%; height:200px; word-break:break-all\">");
context.Response.Write(System.Web.HttpUtility.HtmlEncode(context.Server.GetLastError().ToString()));
context.Response.Write("</textarea>");
context.Response.Write("</body></html>");
context.Response.End();
}
}
#endregion
}
}