473,503 Members | 3,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Label ViewState question

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);
}
Feb 21 '06 #1
5 1767
Hmmm...
Try to create your controls on OnInit() method...

--
Ravi Wallau
no****@nospam.org
"Mark Olbert" <Ch*********@newsgroups.nospam> wrote in message
news:a0********************************@4ax.com...
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);
}

Feb 21 '06 #2
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
Feb 21 '06 #3
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.)


Feb 22 '06 #4
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

On Wed, 22 Feb 2006 03:02:30 GMT, st*****@online.microsoft.com (Steven Cheng[MSFT]) wrote:
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.)


Feb 22 '06 #5
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.)

Feb 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1372
by: Grey | last post by:
I want to know how to dynamically bind the data in label field. My requirement is that I need to display a label with a data which is retrieved from preceding page and I want to use the data...
1
1222
by: ProgramAnalyst | last post by:
Hey all, I have a label that gets its text from a DB. This text can be changed on the page in a textbox and after the save button is clicked the new text is updated in the DB and the page...
1
1757
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
3
3918
by: Kevin Cunningham | last post by:
I have a repeater with some labels in it (code below). For whatever reason the text for the label is not persisted in viewstate on the postback. Is there a trick to get this to work? Is there...
6
6915
by: Joe | last post by:
I know that the Literal control will not render a <span> tag so I can not format its text. Other than this, what is the difference betwen the Literal control and the LiteralControl Control? How...
8
1911
by: Arpan | last post by:
Consider the following code snippet (my main intention is to display the current time in a Label control as & when this ASPX page is accessed/refreshed): <script runat="server"> Class Clock...
16
2337
by: RicercatoreSbadato | last post by:
hi all, i'd like to inherit from a label, how can I do ? i've tried: 1. making a new Web User Control, then inherit from System.Web.UI.WebControls.Label but not works! 2. creating a class...
2
2598
by: Mike P | last post by:
I have a method that is returning a string which can be of any length. Once this string is returned I want to display it on screen in a label control. How can I make the label control's height and...
1
1941
by: Mike P | last post by:
I have a method that is returning a string which can be of any length. Once this string is returned I want to display it on screen in a label control. How can I make the label control's height and...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.