In my last post, I explained the life cycle of a page in ASP.NET. My main purpose was to explain how view state relates to page's life cycle. What is view state supposed to do? View state is responsible for persisiting state of an ASP.NET page and its controls across postbacks. Some developers might think they need to store every state data related to a control in view state. This is not true. For example, the properties that have been declared in the HTML source of the page, do not need to be persisted across postbacks. If your recall from my last post, in page's life cycle, the first stage is instantiation which builds up the control hierarchy. It's where the properties that are defined declaratively, will be assigned to the control in the control hierarchy. Thus, developers do not need to persist such properties. What needs to be persisted in view state is the state that changes programmatically. For instance, if the state of a control is changed in a Button control's click event, it will need to be persisted in view state. Otherwise, the change will be lost through the life cycle of the page.
As an example, suppose you have a page containing a Label control and two Button controls. The Label's BackColor property is declaratively set to Beige. The first Button changes the BackColor property of the Label control to Red. The second Button control only posts the page back. Now, let's walk through the page's life cycle when the page first loads, when the first Button is clicked and when the second one is clicked.
When the page loads, the instantiation occurs and the Label control's BackColor property is set to Beige. Since it is not a postbacl, load view state and save view state stages will not execute. The page will be rendered and the output is a Label with a beige background color.
Now you click the first Button control. The instantiation sets the Label's background color to Beige. Load view state makes no changes, because no save view state happened in the previous page load. After the load event, raise postback event happens which changes the Label's background color to Red. The next stage is save view state. It's when the BackColor property of the Label control will be persisted, because it's a programmatic change. At the end, the result is rendered and sent back to the client's browser.
Now you click the second Button control which posts the page back. The instantiation stage loads the BackColor property of the Label control and sets it to Beige. In load view state stage, the state that was saved in the previous stage, is loaded which means the Label control's BackColor property is set to Red. Then save view state happens exactly as the previous page load. The page is rendered and is transmitted to the browser.
Suppose the BackColor property of the Label control was not persisted. This means the change made by clicking the first Button control wouldn't be loaded after clicking the second Button control and it would be lost. You can try this out by setting the EnableViewState property of the Label control to false. You will notice that after clicking the second Button control, the change is lost and the background color of the Label control is set back to Beige.
There is something I need to point out here. There are certain controls that persist their state even when EnableViewState is set to false. MSDN explains it this way. This behaviour occurs because ViewState of a control is only one of the methods that are used to persist a control's attributes across requests. In TextBox, CheckBox and RadioButton controls, attributes that are not normally posted to the server through the GET and POST methods are handled by ViewState. Attributes that are normally posted to the server are handled by the IPostBackDataHandler interface.
No comments:
Post a Comment