zoukankan      html  css  js  c++  java
  • 关于ASP.NET页面类继承的问题

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



  • 相关阅读:
    在yii中使用Filter实现RBAC权限自动判断
    关于WEB设计透明和阴影
    一句话扯扯数据结构的概念点
    Console API Google 浏览器开发人员工具使用
    git提交项目时候,忽略一些文件
    学习笔记 如何解决IE6 position:fixed固定定位问题{转载}
    [转载]yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换
    Jquery 常用方法经典总结【砖】
    PHP中冒号、endif、endwhile、endfor这些都是什么
    [转载]救命的PHP代码
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/1658304.html
Copyright © 2011-2022 走看看