Reputation: 37660
I have a label within a user control:
<asp:Label runat="server" ID="lblRemainingPlacesMessage" Visible="false" />
I want to setup its visibility to true:
protected void Page_Load(object sender, EventArgs e) {
lblRemainingPlacesMessage.Visible = true;
}
However, the label is still hidden.
What's is puzzling me, is that the property can't be changed, even in the immediate window, or the debugger local watch:
(immediate window)
lblRemainingPlacesMessage.Visible
false
lblRemainingPlacesMessage.Visible = true
true
lblRemainingPlacesMessage.Visible
false
What can explain that my Visible property can't be changed?
I have no exception. Just a NOOP like operation...
My app has the viewstate enabled. Most of all, I have other label in the page, that works perfectly!
Don't know if its matter, but I dynamically instantiate my user control within my owner page using:
protected override void CreateChildControls()
{
m_VisualControl = (MyUserControl)Page.LoadControl(_ascxPath);
Controls.Add(m_VisualControl);
}
The app uses ASP.Net WebForms with .net 3.5 SP1, and I use Visual Studio 2012 Premium.
Upvotes: 3
Views: 2040
Reputation: 37660
Yuriy Galanter's comment put me on the right track.
Simply, my immediate parent was not visible. And I suppose the visible property of a control combines the control ancestors visibility.
Sometimes simple problem have simple resolution :)
Upvotes: 1
Reputation: 6839
1) You should create any dynamic UserControl at OnInit or you will not be able to use ViewState
:
2) Expose the label property that you wan't to change as property of the UserControl
public bool HiddeMyLabel
{
set { lblRemainingPlacesMessage.Visible = value; }
get { lblRemainingPlacesMessage.Visible; }
}
3) You should ever use OnPreRender from the page to change any control property.
protected override void OnPreRender(EventArgs e)
{
MyUserControl.HiddeMyLabel = false;
}
4) If you still having issues, remove the hidden attribute manually:
public bool HiddeMyLabel
{
set
{
if(value)
lblRemainingPlacesMessage.Attributes.Add("style", "display:none");
else
lblRemainingPlacesMessage.Attributes.Add("style", "display:block");
}
}
Upvotes: 0