关于ASP.NET页面类继承的问题

ExpandedBlockStart.gif 代码
// 关于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方法。
    
//



 

转载于:https://www.cnblogs.com/binlyzhuo/archive/2010/01/28/1658304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值