显示列表

经常在基类的构造函数中获取用户的信息,比如Cookie、Session等,但Session在构造时却还不存在。
不过在OnInit时就可读取Session值了。
public partial class BasePage : System.Web.UI.Page
{
    public BasePage()
    {
        // 这里Session对象还未创建
        if (System.Web.HttpContext.Current.Session == null)
        {
            System.Web.HttpContext.Current.Response.Write("Session Is Null");
        }
        else
        {
            System.Web.HttpContext.Current.Response.Write("Session Is Exist");
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        // 这里就可以访问Session对象了
        if (System.Web.HttpContext.Current.Session == null)
        {
            System.Web.HttpContext.Current.Response.Write("Session Is Null");
        }
        else
        {
            System.Web.HttpContext.Current.Response.Write("Session Is Exist");
        }
    }
}

标签:Session 
返回摘要 | 分类(Asp.Net) | 访问(18) | 编辑