Read More on Telerik Blogs
September 26, 2007 Web
Get A Free Trial
As some of us have found last Friday this article in MSDN about how to store your ViewState in Session is totally wrong. Using the Reflector you can find very easily that ControlState will be erased if you access more than once PageStatePersister.

The correct implementation should be:

     PageStatePersister _pers;
     protected override PageStatePersister PageStatePersister
     {
         get
         {
             if (_pers == null)
                 _pers = new SessionPageStatePersister(this);
             return _pers;
         }
     }

How about overriding SavePageStateToPersistenceMedium / LoadPageStateFromPersistenceMedium?
If you do this you should update your PageStatePersister or you will miss the ControlState once again. Example:

protected override object LoadPageStateFromPersistenceMedium()
{
     object state = THESTATE;
     if (state is Pair)
     {
         Pair statePair = (Pair)state;
         PageStatePersister.ControlState = statePair.First;
         PageStatePersister.ViewState = statePair.Second;
     }
     return state;
}

Both implementations are widely spread so be careful!

About the Author

Vladimir Enchev

is Director of Engineering, Native Mobile UI & Frameworks