Label ViewState question

M

Mark Olbert

I have a composite control (under ASPNET2; derived from CompositeControl) which contains a label. The contents of the label are set
when the control is created to a user-supplied value.

What's confusing me is that on postback the contents of the label are not being restored from ViewState. I thought that happened
automagically...but I guess not. I'm obviously missing something really simple. How do I get the label to set its text from
ViewState on postback?

protected override void CreateChildControls()
{
Controls.Clear();

lblCtl = new Label();
lblCtl.ID = "questionLabel";

// UseStoredState is true on postback, false on initial creation
if( !UseStoredState )
{
lblCtl.Text = quesRow.text;
ViewState["questionId"] = quesRow.id.ToString();
}

Controls.Add(lblCtl);
}
 
M

Mark Olbert

Nope, that doesn't solve the problem.

Am I correct that the initialization from ViewState for Label.Text should be occurring automatically when the Label control is
recreated? Maybe my understanding is wrong.

- Mark
 
S

Steven Cheng[MSFT]

Hi Mark,

From the CreateChildControls method you posted, I think your current code
logic is correct except the below line:

===============
if( !UseStoredState )
{
lblCtl.Text = quesRow.text;
ViewState["questionId"] = quesRow.id.ToString();
}
==============

Try avoid access viewstate in CreateChildControls method since it is not
guranteed to be called after ViewState is loaded. In addition, since I do
not know how your control class is declaring, not sure whether you've
implemented the INamingContainer interface, this interface is necessary for
composite control which contains sub controls.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Mark Olbert

Steven,

The class defining my custom WebControl derives from CompositeControl, so the INamingContainer issue should be handled. This is all
under ASPNET2.

Removing the ViewState[] assignment does not solve the problem.

To reiterate, the question is this: under what circumstances will a control, when created as part of a composite control, initialize
itself from ViewState when it is recreated on postback?

The MSDN documentation is unclear to me on the subject. In some places it states that ASPNET server controls configure themselves
from ViewState when they are recreated on postback. In other places properties like Label.Text are explicitly stored and retrieved
to ViewState during control creation and re-creation.

I would appreciate some clarification.

- Mark
 
S

Steven Cheng[MSFT]

Hi Mark,

I've seen your new posts regarding on this issue which contains more
detailed info on this in this newsgroup.

Subject: ViewState not restoring in composite control (ASPNET2)

I've also posted my further suggestion and some sample code there. Please
feel free to post there if you meet any further problem.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Joined
Apr 1, 2008
Messages
1
Reaction score
0
Not able to retain the Label control value at server

Hi,
I am also having similar problem.
Label control value is not maintained, when the page is submitted while the page is loading in the browser. So I am not able to read the value at server for processing. If you have solution


Regards,

Niranjan
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top