1.异常处理思路:遇到异常就直接抛出,然后在Global.asax.cs中的Application_Error方法捕获。捕获到异常之后跳转到异常处理页面即可。
2.假窗口提示:关键还是这个假窗口要做的好看,其它的没什么。
废话不多说,先看效果图:
效果还不错吧。
实现步骤:
1.新建项目TryCatch,添加两个页面WebTest.aspx和Error.aspx。添加类、css、等,结构如下:
2.添加PageBase.cs页面,做为所有页面的基类,代码如下:
3.WebTest.aspx.cs添加事件以及代码,这里要注意,这个页面是继承自
PageBase的,这样这个页面里才有这个提示div
4.Global.asax.cs添加代码,Global.asax的作用不多说了,不知道的去搜索一下。
5.Error.aspx.cs代码:
6.要注意一下Error.log是记录错误日志的,要有写的权限,不然是写不进去的。
上面只是思路,我把Demo放这里给大家下载:
新浪下载 CSDN下载
2.假窗口提示:关键还是这个假窗口要做的好看,其它的没什么。
废话不多说,先看效果图:
效果还不错吧。
实现步骤:
1.新建项目TryCatch,添加两个页面WebTest.aspx和Error.aspx。添加类、css、等,结构如下:
2.添加PageBase.cs页面,做为所有页面的基类,代码如下:
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Web;
- using System.Web.UI;
- using System.Resources;
- using System.IO;
- using System.Reflection;
- using System.Data.SqlClient;
- namespace TryCatch
- {
- /// <summary>
- /// the base class for Page。
- /// </summary>
- public class PageBase : System.Web.UI.Page
- {
- #region 本类构造函数
- public PageBase()
- {
- //只有有页面继承这个类,就会为这个类的子页面自动加上这个方法
- RegisterAdminPageClientScriptBlock();
- }
- #endregion
- #region 操作提示
- public void RegisterAdminPageClientScriptBlock()
- {
- string script = "<script language=/"javascript/"> /r/n" +
- "var spath=''/r/n" +
- "var bar=0/r/n" +
- /*"function count() /r/n" +
- "{ /r/n" +
- " bar=bar+1; /r/n" +
- " if (bar<100) { /r/n" +
- " if ( document.getElementById('success').style.display !=/"none/") /r/n" +
- " setTimeout(/"count()/",2000); /r/n" +
- " } /r/n" +
- " else { document.getElementById('success').style.display =/"none/"; } /r/n" +
- "} /r/n" +*/
- "function run(spath) /r/n" +
- "{ /r/n" +
- " if (typeof(Page_ClientValidate) == 'function') /r/n" +
- " { /r/n" +
- " if(Page_ClientValidate()) /r/n" +
- " { /r/n" +
- " document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=/"0/" src='+spath+' /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR />';/r/n" +
- " document.getElementById('success').style.display = /"block/"; /r/n" +
- //" setInterval('count()',20000); /r/n" +
- " } /r/n" +
- " } /r/n" +
- " else /r/n" +
- " { /r/n" +
- " document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=/"0/" src='+spath+' /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR />';/r/n" +
- " document.getElementById('success').style.display = /"block/"; /r/n" +
- //" setInterval('count()',20000); /r/n" +
- " } /r/n" +
- "} /r/n" +
- "</script>/r/n"+
- "<div id=/"success/" style=/"position:absolute;z-index:300;height:120px;width:284px;left:50%;top:50%;margin-left:-150px;margin-top:-80px;/">/r/n" +
- " <div id=/"Layer2/" style=/"position:absolute;z-index:300;width:270px;height:90px;background-color: #FFFFFF;border:solid #000000 1px;font-size:14px;/">/r/n" +
- //" <div id=/"Layer4/" style=/"height:26px;background:"+style+"line-height:26px;padding:0px 3px 0px 3px;font-weight:bolder;/">"+RM.GetString("OpTitle")+"</div>/r/n" +
- " <table border=0 cellpadding=0 cellspacing=0 width=270px><tr style=/"height:24px;background:#9FC4FB;line-height:24px;padding:0px 3px 0px 3px;font-weight:bolder;font-size:14px;/"><td style=/"font-size:14px;/">提示</td> <td align=/"right/"><font style=/"cursor:pointer/" color=/"#ffffff/" face=/"webdings/" οnclick=/"document.getElementById('success').style.display ='none';/">r</font></td> </tr></table>/r/n" +
- " <div id=/"Layer5/" style=/"height:64px;line-height:150%;padding:0px 3px 0px 3px;/" align=/"center/"><BR /><table><tr><td valign=top><img border=/"0/" src=/"../images/ajax_loading.gif/" /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR /></div>/r/n" +
- " </div>/r/n" +
- " <div id=/"Layer3/" style=/"position:absolute;width:270px;height:90px;z-index:299;left:4px;top:5px;background-color: #E8E8E8;/"></div>/r/n" +
- "</div>/r/n" +
- "<script> /r/n" +
- "document.getElementById('success').style.display = /"none/"; /r/n" +
- "</script> /r/n" ;
- //"<script language=/"JavaScript1.2/" src=/"../js/divcover.js/"></script>/r/n";
- base.RegisterClientScriptBlock("Page", script);
- }
- #endregion
- }
- }
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace TryCatch
- {
- /// <summary>
- /// WebTest 的摘要说明。
- /// </summary>
- public class WebTest : PageBase
- {
- protected System.Web.UI.WebControls.Button Button2;
- protected System.Web.UI.WebControls.Button Button1;
- private void Page_Load(object sender, System.EventArgs e)
- {
- if (!IsPostBack)
- {
- string strpath="images/ajax_loading.gif";
- Button1.Attributes.Add("onmouseup","run('"+strpath+"');");
- Button2.Attributes.Add("OnBlur","document.getElementById('success').style.display ='none';");
- }
- }
- private void Button1_Click(object sender, System.EventArgs e)
- {
- try
- {
- //为了能看到提示效果,这里终止当前线程2秒
- System.Threading.Thread.Sleep(2000);
- for (int i=2;i>-1;i--)
- {
- //会报一个不能除0的异常
- int j = 10/i;
- }
- }
- catch (System.Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- private void Button2_Click(object sender, System.EventArgs e)
- {
- throw new Exception("我是异常信息");
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Button1.Click += new System.EventHandler(this.Button1_Click);
- this.Button2.Click += new System.EventHandler(this.Button2_Click);
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- }
- }
4.Global.asax.cs添加代码,Global.asax的作用不多说了,不知道的去搜索一下。
- protected void Application_Start(Object sender, EventArgs e)
- {
- //先初始化一下
- Application["msg"]=1;
- Application["tag"]=1;
- }
- protected void Application_Error(Object sender, EventArgs e)
- {
- try
- {
- //开始异常捕获了,这个判断是防止死锁的,如果不加会循环调用
- if(Convert.ToInt32(Application["tag"])>2)
- {
- Application["tag"]=1;
- return;
- }
- Application["tag"]=Convert.ToInt32(Application["tag"])+1;
- Exception objErr = Server.GetLastError().GetBaseException();
- Application["errorPage"] = Request.Url.ToString();
- Application["errorMsg"] =objErr.Message;
- Server.ClearError();
- Response.Redirect("Error.aspx");
- }
- catch{}
- }
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace TryCatch
- {
- /// <summary>
- /// Error 的摘要说明。
- /// </summary>
- public class Error : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.Label lblError;
- private void Page_Load(object sender, System.EventArgs e)
- {
- if(!IsPostBack)
- {
- try
- {
- Application["tag"]=1;
- lblError.Text="发生异常页:"+Application["errorPage"].ToString()+"<br>异常信息:"+Application["errorMsg"].ToString();
- MessageBox.ShowPopupWin(this,"","",50000);
- WriterLog log = new WriterLog( Server.MapPath(@"./Log/Error.log"));
- log.WriterError(Application["errorPage"].ToString(),Application["errorMsg"].ToString());
- }
- catch{}
- }
- }
- #region Web 窗体设计器生成的代码
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.Load += new System.EventHandler(this.Page_Load);
- }
- #endregion
- }
- }
上面只是思路,我把Demo放这里给大家下载:
新浪下载 CSDN下载