Programming Microsoft ASP.NET 4 - Dino Esposito [368]
The View State of a Page
ASP.NET pages supply the ViewState property to let applications build a call context and retain values across two successive requests for the same page. The view state represents the state of the page when it was last processed on the server. The state is persisted—usually, but not necessarily, on the client side—and is restored before the page request is processed.
By default, the view state is maintained as a hidden field added to the page. As such, it travels back and forth with the page itself. Although it is sent to the client, the view state does not represent, nor does it contain, any information specifically aimed at the client. The information stored in the view state is pertinent only to the page and some of its child controls and is not consumed in any way by the browser.
The view state comes at a cost. At the same time, however, the view state is one of the most important features of ASP.NET, not so much because of its technical relevance but because it allows you to benefit from most of the magic of the Web Forms model. Used without strict criteria, though, the view state can easily become a burden for pages.
The StateBag Class
The StateBag class is the class behind the view state that manages the information that ASP.NET pages and controls want to persist across successive posts of the same page instance. The class works like a dictionary and, in addition, implements the IStateManager interface. The Page and Control base classes expose the view state through the ViewState property. So you can add or remove items from the StateBag class as you would with any dictionary object, as the following code demonstrates:
ViewState["FontSize"] = value;
You should start writing to the view state only after the Init event fires for the page request. You can read from the view state during any stage of the page life cycle, but not after the page enters rendering mode—that is, after the PreRender event fires.
View State Properties
Table 17-14 lists all the properties defined in the StateBag class.
Table 17-14. Properties of the StateBag Class
Property
Description
Count
Gets the number of elements stored in the object.
Item
Indexer property. It gets or sets the value of an item stored in the class.
Keys
Gets a collection object containing the keys defined in the object.
Values
Gets a collection object containing all the values stored in the object.
Each item in the StateBag class is represented by a StateItem object. An instance of the StateItem object is implicitly created when you set the Item indexer property with a value or when you call the Add method. Items added to the StateBag object are tracked until the view state is serialized prior to the page rendering. Items serialized are those with the IsDirty property set to true.
View State Methods
Table 17-15 lists all the methods you can call in the StateBag class.
Table 17-15. Methods of the StateBag Class
Method
Description
Add
Adds a new StateItem object to the collection. If the item already exists, it gets updated.
Clear
Removes all items from the current view state.
GetEnumerator
Returns an object that scrolls over all the elements in the StateBag.
IsItemDirty
Indicates whether the element with the specified key has been modified during the request processing.
Remove
Removes the specified object from the StateBag object.
The IsItemDirty method represents an indirect way to call into the IsDirty property of the specified StateItem object.
Note
The view state for the page is a cumulative property that results from the contents of the ViewState property of the page plus the view state of all the controls hosted in