Solved

Viewstate with Custom Controls

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now