Session is null when inherit from System.Web.UI.Page

Posted by Andreas K. on Stack Overflow See other posts from Stack Overflow or by Andreas K.
Published on 2010-06-16T14:31:34Z Indexed on 2010/06/16 14:32 UTC
Read the original article Hit count: 237

Filed under:
|
|

I want to extend the System.Web.UI.Page-class with some extra stuff. In the ctor I need the value of a session-variable.

The problem is that the Session-object is null...

public class ExtendedPage : System.Web.UI.Page {
   protected foo;   

   public ExtendedPage() {
      this.foo = (int)HttpContext.Current.Session["foo"];   // NullReferenceException
   }
}

If I move the part with the session-object into the Load-Event everything works fine...

public class ExtendedPage : System.Web.UI.Page {
   protected foo;

   public ExtendedPage() {
      this.Load += new EventHandler(ExtendedPage_Load);
   }

   void ExtendedPage_Load(object sender, EventArgs e) {
      this.foo = (int)HttpContext.Current.Session["foo"];
   }
}

Why is the Session-object null in the first case??

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET