Solved

Viewstate with Custom Controls

Posted on 2003-12-05
5
586 Views
Last Modified: 2010-08-05
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

0
Comment
Question by:devaid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9882243
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.
0
 
LVL 2

Author Comment

by:devaid
ID: 9882521
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.
0
 
LVL 2

Author Comment

by:devaid
ID: 9882762
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
0
 
LVL 18

Accepted Solution

by:
testn earned 500 total points
ID: 9884211
Absolutely,

instead of create your control in Page_Load. You should create your control in CreateChildControls instead.

override protected void CreateChildControls() {
    CustControl = myCustC = new CustControl (3);
    ...
    this.Controls.Add(CustControl);
}

private void Page_Load(object sender, EventArgs e) {
    EnsureChildControls();
    ... do you stuff here
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question