views:

548

answers:

1

Could anybody explain me how it works.

I have a page, which has button inside the update panel and some class member (_pageContext). It is not static member, by the way That's its declaration

    internal PageContext _pageContext = null;

When I click the button, next events occurs (I trace them making breakpoints)

1) Page constructor. _pageContext == null at the breakpoint, ok 2) OnInit(). Here weird things begin. _pageContext is already initialized (though its initialization method also is breakpointed, and that breakpoint has never been hit).

How can it happen and where new instance of page class can know this _pageContext from, though it has never been initialized ? It makes sense that the previous instance of class is recovered at On_Init partial postback, though it is impossible since HTTP is stateless and browser has no connection with server class instances.

Any suggestions and explanations of this supernatural fact?

A: 

HTTP is stateless indeed. ASP.NET WebForms abstracts that away from you with the cunning use of ViewState. Sounds like pageContext is being ViewStated into existence - look at this for more info.

And don't worry if it isn't obvious at first - understanding lifecycle is not easy. Nuh-uh.

Gabriel Florit