Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 19, 2012 07:00 PM by gmorris
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.
<asp:Label id="lblLastNotification" runat="server" CssClass="text" Text="07/05/05 09:45 am"></asp:Label>
Page_Load(object sender, System.EventArgs e)
// lblLastNotification not set here
// occurs after Page_Load(), called by TreeView event when first loaded
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)?
Apr 28, 2011 06:24 AM|LINK
Set the viewstate property for that label control.
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.
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?
Apr 28, 2011 02:31 PM|LINK
When I view source, the control is present as:
<span id="_ctl0_ContentPlaceHolder1_TimeClock1_UltraWebTab1__ctl0_TimeClockDetails1_lblLastNotification" class="text">01/01/00 12:00 AM</span>
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?
Apr 28, 2011 03:51 PM|LINK
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).
Apr 28, 2011 04:54 PM|LINK
There is no 'viewstate' property. EnableViewState for the page & the control are both set to true.
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?