我希望扩展System.Web.UI.Page类以添加一些额外的内容。在构造函数中,我需要一个会话变量的值。
问题是Session对象为空...
如果我将带有会话对象的部分移到Load事件中,一切都能正常工作...
为什么第一个情况下会出现 Session 对象null的情况?
问题是Session对象为空...
public class ExtendedPage : System.Web.UI.Page {
protected foo;
public ExtendedPage() {
this.foo = (int)HttpContext.Current.Session["foo"]; // NullReferenceException
}
}
如果我将带有会话对象的部分移到Load事件中,一切都能正常工作...
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"];
}
}
为什么第一个情况下会出现 Session 对象null的情况?