Solved

Viewstate with Custom Controls

Posted on 2003-12-05
5
587 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

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