ASP.NET ViewState

Background.
ViewState is a collection of data which enables a control/page etc to persist properties across postbacks.

Like a HashTable, the ViewState contains key value pairs.

The ViewState is represented across posts by a hidden form field.

The data stored within this hidden field is referred to as a StateBag.

TrackViewState

The ViewState object exposes a method called TrackViewState.

Only after this method is called upon the ViewState, are items stored within the ViewState marked as dirty when a keys value is changed.

New key value pairs will also be marked as dirty from this point.

TrackViewState and the page/control life cycle

TrackViewState is called during OnInit.

After this point, data in the StateBag is applied to the ViewState.

This means that if you modify the ViewState after OnInit, the data will be persisted in the StateBag and will be available after a subsequent postback.

However if you modify the ViewState before OnInit, or during OnInit your changes will NOT be stored in the StateBag.

Posted in Labels: , |

0 comments: