我自己定义了个类PageSession,继承System.Web.UI.Page构造方法里面判断HttpContext.Current.Session["Islogin"]是否为空 if (HttpContext.Current.Session["Islogin"]==null)//未将对象引用设置到对象的实例。
{
                HttpContext.Current.Response.Write("session Is Null");
}
 else
{
                HttpContext.Current.Response.Write("session Is NotNull");
}
以后需要session判断的页都继承此类PageSession但运行的时候一直报错。未将对象引用设置到对象的实例。还有想问问这两者的区别,谢谢

解决方案 »

  1.   

    HttpContext.Current.Session
    这个通常用在单独的类里
    Page.Session一般是页面的后置类里用.
      

  2.   


    那HttpContext.Current.Session 
    和Page.Session相等吗?
      

  3.   

    是相等的,把你自定义的PageSession 继承 IRequiresSessionState 接口,再试试 HttpContext.Current.Session["Islogin"]是否为null
      

  4.   

    使用 System.Web.HttpContext.Current.Session 访问当前Web 应用程序的Session。通常我们是使用 Page.Session(在网页的后台代码中)或 context.Session(来访问当前Web应用程序的 Session。但是在类库中,我们无法获取 Page 变量或 context 变量,这时我们就可以使用System.Web.HttpContext.Current.Session 访问当前Web 应用程序的Session而这完全相同
      

  5.   

    public class PageSession : System.Web.UI.Page, IRequiresSessionState 
        {
            public PageSession()
            {
                if (HttpContext.Current.Session["Islogin"] == null)//为什么这里一直报错未将对象引用设置到对象的实例。
                {
                    HttpContext.Current.Response.Write("session Is Null");
                }
                else
                {
                    HttpContext.Current.Response.Write("session Is NotNull");
                }
            }
        }
      

  6.   


    if (HttpContext.Current.Session["Islogin"] == null)
                {
                    Response.Write("ddd");
                }在page里这样不报错
      

  7.   


    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            string sid = Convert.ToString(System.Web.HttpContext.Current.Session["Employee_Code"]);//使用字符串转换
     if (sid == null || sid == "")
    {}
      

  8.   


    在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以确定处理程序是否需要对会话状态值具有读写访问权
      

  9.   

    帮你翻了一下,可以参考下这个地址:http://blog.joycode.com/saucer/archive/2005/08/25/62473.joy
      

  10.   

    应该是继承System.Web.UI.Page类时写错了!
    你从头查下程序。未将对像引用到实例可能是由于其它地方出问题导至的
      

  11.   

    太早了,Session对象没创建,你应该重写Page的OnInit或者OnLoad方法,把你的代码放到这里边。www.webdiyer.com
      

  12.   

    哎,又要从asp.net的生命周期开始算了,牙卖爹
      

  13.   

    14楼说的对,放先放onLoad方法里
      

  14.   

    构造函数执行在asp.net页面所有的生命周期之前,所以Session还没创建呢
    在重写OnLoad事件中处理吧
      

  15.   

    终于搞定了谢谢大家 protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                if (System.Web.HttpContext.Current.Session["Islogin"] == null)
                {
                    HttpContext.Current.Response.Write("session Is Null");
                }
                else
                {
                    HttpContext.Current.Response.Write("session Is NotNull");
                }
            }
      

  16.   

    弱弱的问下  那我在global.asax里面怎么调用 OnLoad(EventArgs e)