2010-06-16 12 views
3

몇 가지 추가 작업으로 System.Web.UI.Page 클래스를 확장하려고합니다. ctor에서 세션 변수의 값이 필요합니다.System.Web.UI.Page에서 상속하면 세션이 null입니다.

문제는 ... 내가로드 이벤트 모두에 세션 객체 부분을 이동하면

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

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

... 세션 객체가 null이다 잘 작동

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"]; 
    } 
}

왜 세션 객체가 null 첫 번째 경우에 ??

답변

5

Page 개체가 생성 된 후에 Page lifecycle에 나중에 Session 속성이 설정됩니다.

+0

구성시 설정되지 않았지만 페이지로드 전에 설정된다는 것을 의미합니까? (전 사전 준비에서 생각합니다.) –

+1

@ 스벤 : 맞아. – SLaks

0

.aspx 페이지에서 ExtendedPage 클래스를 상속해야합니다.

HttpContext에서 실행될 때 Session을 갖습니다.

+0

'Load' 이벤트가 발생하면 그는 이미 그렇게하고 있습니다. – SLaks

관련 문제