I have a label on an ascx page. This page is loaded as a user control as the result of a treenode selection, so always has IsPostBack == true. Therefore I don't set the label.text in Page_Load. There is a separate method called Populate() which populates
several text boxes and one label. When I hit the Save button on the parent page, Page_Load() fires again, then the button event. The data is read back from the text boxes and saved to the database. But the label's value is not restored. I do not have EnableViewState set
anywhere, so the page and the control defaults should all be true.
Page_Load(object sender, System.EventArgs e)
{
// lblLastNotification not set here
}
// occurs after Page_Load(), called by TreeView event when first loaded
Populate()
{
...
lblLastNotification.Text = timeClockContainer.LastHeartBeatDateTime;
...
}
When I change it, it should be marked as dirty & saved in the ViewState. Why is this label losing the text value (or why is it not saved in ViewState)?
find all the reference of lblLastNotification and see that if you are assigning "" in your code any where if yes. then add a breakpoint on that line and run your code. Also try adding EnableViewState="true" in your label tag.
Thanks,
Zeeshan Umar ~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
There are two references to lblLastNotification in the program: you're lookin' at 'em above. This is a read-only status value set once. EnableViewState is true when I check it during the Save button postback. Something is seriously wrong. I have another
ascx child page that will not maintain postback state for checkboxes.
Is there any setting for VS2005 or IE7 that would cause this?
No, as I stated above, there are only two references to this control: in the markup, and in the Populate method (called after Page_Load when ViewState tracking should be on).
One thing I did notice: when I click the 'Save' button, the label AS POSTED BACK has the markup-defined value in it, not the value I saved in it when I first populated it. To recap, here are the steps:
The page is loaded, and Populate() method is called. After postback, on client, label shows "1/1/2000 12:00 am" (or some other date/time) like it should.
When I click the 'Save' button, within the Save() postback handler I check the value of the label.Text and it has the markup value "07/05/05 09:45 am". Clearly the value is not being saved in ViewState.
Somewhere I read that I have to use a hidden control to save the value of a label's text between postbacks. Is that true? If so, then why does it even have an EnableViewState property if it doesn't do anything?
gmorris
Member
31 Points
23 Posts
Label Viewstate and Postbacks
Apr 27, 2011 05:02 PM|LINK
I have a label on an ascx page. This page is loaded as a user control as the result of a treenode selection, so always has IsPostBack == true. Therefore I don't set the label.text in Page_Load. There is a separate method called Populate() which populates several text boxes and one label. When I hit the Save button on the parent page, Page_Load() fires again, then the button event. The data is read back from the text boxes and saved to the database. But the label's value is not restored. I do not have EnableViewState set anywhere, so the page and the control defaults should all be true.
Page_Load(object sender, System.EventArgs e) { // lblLastNotification not set here } // occurs after Page_Load(), called by TreeView event when first loaded Populate() { ... lblLastNotification.Text = timeClockContainer.LastHeartBeatDateTime; ... }When I change it, it should be marked as dirty & saved in the ViewState. Why is this label losing the text value (or why is it not saved in ViewState)?
harieis
Participant
782 Points
242 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 06:24 AM|LINK
Set the viewstate property for that label control.
Harikrishnan.S
sirdneo
All-Star
15171 Points
2509 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 09:44 AM|LINK
find all the reference of lblLastNotification and see that if you are assigning "" in your code any where if yes. then add a breakpoint on that line and run your code. Also try adding EnableViewState="true" in your label tag.
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
gmorris
Member
31 Points
23 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 01:47 PM|LINK
There are two references to lblLastNotification in the program: you're lookin' at 'em above. This is a read-only status value set once. EnableViewState is true when I check it during the Save button postback. Something is seriously wrong. I have another ascx child page that will not maintain postback state for checkboxes.
Is there any setting for VS2005 or IE7 that would cause this?
gmorris
Member
31 Points
23 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 02:31 PM|LINK
When I view source, the control is present as:
It has the correct text in it now. After postback, it has reverted to '07/05/05 9:45 AM'. Doesn't make any sense.
One thing I'm not clear on. Is the label text stored as part of the markup, or in ViewState, or both? What about when it's changed/dirty?
sirdneo
All-Star
15171 Points
2509 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 03:51 PM|LINK
Are you changing the value of label through javascript? if yes then it will always show old value after postback.
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
gmorris
Member
31 Points
23 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 04:52 PM|LINK
No, as I stated above, there are only two references to this control: in the markup, and in the Populate method (called after Page_Load when ViewState tracking should be on).
gmorris
Member
31 Points
23 Posts
Re: Label Viewstate and Postbacks
Apr 28, 2011 04:54 PM|LINK
There is no 'viewstate' property. EnableViewState for the page & the control are both set to true.
gmorris
Member
31 Points
23 Posts
Re: Label Viewstate and Postbacks
Dec 19, 2012 07:00 PM|LINK
One thing I did notice: when I click the 'Save' button, the label AS POSTED BACK has the markup-defined value in it, not the value I saved in it when I first populated it. To recap, here are the steps:
The page is loaded, and Populate() method is called. After postback, on client, label shows "1/1/2000 12:00 am" (or some other date/time) like it should.
When I click the 'Save' button, within the Save() postback handler I check the value of the label.Text and it has the markup value "07/05/05 09:45 am". Clearly the value is not being saved in ViewState.
The control is currently defined as:
<asp:Label id="lblLastNotification" runat="server" CssClass="text" Text="07/05/05 09:45 am" EnableViewState="true"></asp:Label>
Somewhere I read that I have to use a hidden control to save the value of a label's text between postbacks. Is that true? If so, then why does it even have an EnableViewState property if it doesn't do anything?