Can't access label in MasterPage

  • Thread starter Thread starter clintonG
  • Start date Start date
C

clintonG

I'm having a problem with a label in a MasterPage which is not in a
ContentPlaceHolder. The naughty label will not allow me to change its text
property.

<%-- renders the label's text directly to the page --%>
<div id="footer">
<asp:Label ID="CurrentTimeLabel" runat="server" Text="CurrentTimeLabel">
</div>

When the control is not in a ContentPlaceHolder I should be able to access
the control directly...

// direct access
CurrentTimeLabel.Text = DateTime.Now.ToString();

When the page is rendered, the label control emits a span in the HTML
source but no text property! So I tried getting a reference to the control.

// should get a reference to a label control that is not in a
ContentPlaceHolder
Label currentTime = (Label)Master.FindControl("CurrentTimeLabel");
currentTime.Text = "Found CurrentTimeLabel";

Still no text being rendered! This is very strange for me. I don't
understand why all of a sudden I can't access the property of a label
in the MasterPage.

<%= Clinton Gallagher
 
Referencing the control from the Page_PreRender event resolves this issue...

<%= Clinton Gallagher
 
Back
Top