代码
//
关于ASP.NET页面类继承的问题
// ASP.NET的后台代码CS是一个继承于System.Web.UI.Page的类
// 如果想写一公共的方法,如用Cookie或者Session判断用户是否登录系统.
// 可自定义一个继承于System.Web.UI.Page的类
// 例如:
namespace HttpContextProj
{
public class MyPage:System.Web.UI.Page
{
public MyPage()
{
// ??
}
}
}
// 在新建的页面的后台代码继承这个类;例如:
public partial class _Default :MyPage
{
protected void Page_Load( object sender, EventArgs e)
{
//
// HttpContext.Current.Session["name"] = "123";
}
}
// 在Page_Load方法执行以前很多的Page对象是不能用的,例如Cookie,Application,Session
// 要解决这个问题要在自定义的类中重写Page_Load方法,OnInit()和InitializeComponent方法.
// 完整的自定义类
public class MyPage:System.Web.UI.Page
{
public MyPage()
{
// ??
}
#region 页面载入
private void Page_Loadx( object sender, System.EventArgs e)
{
if (HttpContext.Current.Session[ " name " ] == null )
{
HttpContext.Current.Response.Redirect( " http://www.google.cn/ " , true );
}
}
#endregion
#region 初始化
protected override void OnInit(EventArgs e)
{
base .OnInit(e);
InitializeComponent(); // ??
}
#endregion
#region 载入组件
private void InitializeComponent()
{
this .Load += new EventHandler(Page_Loadx);
}
#endregion
}
// 在实例化_Default类的时候首先执行的是OnInit方法。它重写了System.Web.UI.Page.OnInit。
// 然后调用InitializeComponent方法,而InitializeComponent方法又调用了Load事件,执行了Page_Load方法。
//
// ASP.NET的后台代码CS是一个继承于System.Web.UI.Page的类
// 如果想写一公共的方法,如用Cookie或者Session判断用户是否登录系统.
// 可自定义一个继承于System.Web.UI.Page的类
// 例如:
namespace HttpContextProj
{
public class MyPage:System.Web.UI.Page
{
public MyPage()
{
// ??
}
}
}
// 在新建的页面的后台代码继承这个类;例如:
public partial class _Default :MyPage
{
protected void Page_Load( object sender, EventArgs e)
{
//
// HttpContext.Current.Session["name"] = "123";
}
}
// 在Page_Load方法执行以前很多的Page对象是不能用的,例如Cookie,Application,Session
// 要解决这个问题要在自定义的类中重写Page_Load方法,OnInit()和InitializeComponent方法.
// 完整的自定义类
public class MyPage:System.Web.UI.Page
{
public MyPage()
{
// ??
}
#region 页面载入
private void Page_Loadx( object sender, System.EventArgs e)
{
if (HttpContext.Current.Session[ " name " ] == null )
{
HttpContext.Current.Response.Redirect( " http://www.google.cn/ " , true );
}
}
#endregion
#region 初始化
protected override void OnInit(EventArgs e)
{
base .OnInit(e);
InitializeComponent(); // ??
}
#endregion
#region 载入组件
private void InitializeComponent()
{
this .Load += new EventHandler(Page_Loadx);
}
#endregion
}
// 在实例化_Default类的时候首先执行的是OnInit方法。它重写了System.Web.UI.Page.OnInit。
// 然后调用InitializeComponent方法,而InitializeComponent方法又调用了Load事件,执行了Page_Load方法。
//