Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Viewstate with Custom Controls

Posted on 2003-12-05
5
Medium Priority
?
589 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

596 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