To understand why saving the Label's changed Text property in the view state is vital, consider what would happen if this information were not persisted in view state. That is, imagine that in step 2's save view state stage, no view state information was persisted. If this were the case, then in step 3 the Label's Text property would be assigned to "Hello, World!" in the instantiation stage, but would not be reassigned to "Goodbye, Everyone!" in the load view state stage. Therefore, from the end user's perspective, the Label's Text property would be "Goodbye, Everyone!" in step 2, but would seemingly be reset to its original value ("Hello, World!") in step 3, after clicking the Empty Postback button.
Friday, April 2, 2010
Role of View State
View state's purpose in life is simple: it's there to persist state across postbacks. (For an ASP.NET Web page, its state is the property values of the controls that make up its control hierarchy.) This begs the question, "What sort of state needs to be persisted?" To answer that question, let's start by looking at what state doesn't need to be persisted across postbacks. Recall that in the instantiation stage of the page life cycle, the control hierarchy is created and those properties that are specified in the declarative syntax are assigned. Since these declarative properties are automatically reassigned on each postback when the control hierarchy is constructed, there's no need to store these property values in the view state.
ASP.NET Page life cycle
Each time a request arrives at a Web server for an ASP.NET Web page, the first thing the Web server does is hand off the request to the ASP.NET engine. The ASP.NET engine then takes the request through a pipeline composed of numerous stages, which includes verifying file access rights for the ASP.NET Web page, resurrecting the user's session state, and so on. At the end of the pipeline, a class corresponding to the requested ASP.NET Web page is instantiated and the
Diagram showing all the stages of a Page Life Cycle:
All the stages of Page Life Cycle and its relevant methods used are:
(1) Page Initialization = Page_Init
(2) View State Loading = LoadViewState
(3) PostBack data processing = LoadPostData
(4) Page Loading = Page_Load
(5) PostBack Change Notification = RaisePostDataChangedEvent
(6) PostBack Event Handling = RaisePostBackEvent
(7) Page Pre Rendering Phase = Page_PreRender
(8) View State Saving = SaveViewState
(9) Page Rendering = Page_Render
(10) Page Unloading = Page_UnLoad
ProcessRequest()
method is invoked Diagram showing all the stages of a Page Life Cycle:
All the stages of Page Life Cycle and its relevant methods used are:
(1) Page Initialization = Page_Init
(2) View State Loading = LoadViewState
(3) PostBack data processing = LoadPostData
(4) Page Loading = Page_Load
(5) PostBack Change Notification = RaisePostDataChangedEvent
(6) PostBack Event Handling = RaisePostBackEvent
(7) Page Pre Rendering Phase = Page_PreRender
(8) View State Saving = SaveViewState
(9) Page Rendering = Page_Render
(10) Page Unloading = Page_UnLoad
Subscribe to:
Posts (Atom)