- using System.Web;
- using System.Web.UI;
- using System.Security;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- /// <summary>
- ///BasePage 的摘要说明
- /// </summary>
- public class BasePage : Page //该类继承自页面最基本的Page类
- {
- public BasePage()
- {
- this.Load += new EventHandler(YanZheng); //向该类的Load事件中委托新事件,Yanzheng。
- }
- protected void YanZheng(object sender, EventArgs e) //验证事件的具体实现方法
- {
- if (Session["UserName"] == null)
- {
- Response.Redirect("login.aspx");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class test : BasePage //这里不再继承自Web.UI.Page,而是继承自BasePage。但是BasePage继承自Web.UI.Page,所以子类也继承该类。
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
或
public class PageBase:System.Web.UI.Page
{
public PageBase()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(PageBase_Load);
this.Error += new System.EventHandler(PageBase_Error);
}
//错误处理
protected void PageBase_Error(object sender, System.EventArgs e)
{
string errMsg = string.Empty;
Exception currentError = HttpContext.Current.Server.GetLastError();
errMsg += "<h1>系统错误:</h1><hr/>系统发生错误, " +
"该信息已被系统记录,请稍后重试或与管理员联系。<br/>" +
"错误地址: " + Request.Url.ToString() + "<br/>" +
"错误信息: " + currentError.Message.ToString() + "<hr/>" +
"<b>Stack Trace:</b><br/>" + currentError.ToString();
HttpContext.Current.Response.Write(errMsg);
Server.ClearError();
}
private void PageBase_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (HttpContext.Current.Session["username"] != null)
{
HttpContext.Current.Response.Write("搜索吧sosuo8.com登陆测试");
}
else
{
HttpContext.Current.Response.Write("你不是阿会楠,不要登陆");
}
}
}
}