Link to home
Start Free TrialLog in
Avatar of devaid
devaid

asked on

Viewstate with Custom Controls

Hi people,

I've created a custom control with 3 textboxes amongst otherstuff.

Onpostback my controls lose their values. I've played with the StateBag / Viewstate but cannot get it to work.

Any places I could read up on or any sugested ideas.
Code Snippets

private bool _isTrackingViewState = true;
private StateBag _viewState;

            #region IStateManager Members

            protected override StateBag ViewState
            {
                  get
                  {
                        if(_viewState == null)
                        {
                              _viewState = new StateBag(false);
                              if(_isTrackingViewState)
                              {
                                    ((IStateManager)_viewState).TrackViewState();
                              }
                        }

                        return _viewState;
                  }
            }

            void IStateManager.TrackViewState()
            {
                  // TODO:  Add FAQ.TrackViewState implementation
                  if(_viewState != null)
                  {
                        ((IStateManager)_viewState).TrackViewState();
                  }
                  _isTrackingViewState = true;
            }

            bool IStateManager.IsTrackingViewState

            {
                  get
                  {
                        return _isTrackingViewState;
                  }
            }

            object IStateManager.SaveViewState()
            {
                  // TODO:  Add FAQ.SaveViewState implementation
                  if (_viewState != null)
                  {
                        return ((IStateManager)_viewState).SaveViewState();
                  }
                  return null;
            }

            void IStateManager.LoadViewState(object savedState)
            {
                  // TODO:  Add FAQ.LoadViewState implementation
                  if(savedState != null)
                  {
                        ((IStateManager)ViewState).LoadViewState(savedState);
                  }
            }

            #endregion

Avatar of testn
testn

Actually you should nnot have to override anything. It should work out of the box.....
Can you post your code? I think there might be something wrong.
Avatar of devaid

ASKER

The override was a stab in the dark as I couldn't get it to work.
The code just generates 3 textboxes with some table layouts and text.
Avatar of devaid

ASKER

Ah ha,
figured out why.

I'm creating the objects at runtime like this:

CustControl = myCustC = new CustControl (3);

So at runtime it creates an instance on each postback thus not ViewState Data.
Any ways around this? --- Increased points from 250 to 500
ASKER CERTIFIED SOLUTION
Avatar of testn
testn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial