Friday, September 16, 2005


StateBag contains key-value pairs.

When you set a value for any key, it first checks to see if IsTrackingViewState is true or not.
[IsTrackingViewState is false by default. It is set to true when the TrackViewState() method is called]
If IsTrackingViewState returns true, the item that is being set is marked as dirty.

SaveViewState() method, when called, takes all dirty items and saves their values and returns that saved object of type "object array".

So, even though you add items to the StateBag, only those items that were set AFTER TrackViewState() has been called will be saved across postbacks.

In Page class, ViewState object if of type StateBag.
Right after the Init stage, the TrackViewState() method is called on all the Page's child controls and on the ViewState StateBag also.

In Custom controls, when ever a child control is added to the Custom Control's ChildControl collection, the Controls.Add method itself calles the TrackViewState() method on the newly added child control.
So, if you want to assign some values to the child control only once and let the control load these values from teh ViewState bag, every time the page is posted back, you MUST assign those values AFTER you have added the control to the Custom control's child collection.

Controls.Add(btnNext); // --- (A)
btnNext.CommandName = "Some_Command_Name"; // Do this AFTER adding btnNext to the Child Controls collection (A), if you want that CommandName value to be loaded in the ViewState bag.


Post a Comment

<< Home