Fermin Fermin - 2 months ago 10
ASP.NET (C#) Question

ASP.NET control with CSS visibility:hidden, not being shown on Control.Visible = true

I have a few labels on my page with a class of 'error', the rule for .error is:

.error {
color:Red;
visibility:hidden
}


The markup for the labels is:

<asp:Label ID="lblError" runat="server" CssClass="error" ></asp:Label>


I then set the .Text property of the error label in my code behind.

If I use
lblError.Visible = True
when I set the text, the label isn't shown. Any ideas why that would be? I'm maybe wrong here but I thought that setting .Visible was like setting the visibility style?

Answer

The Visible property affects rendering of the entire element and is unrelated to the CSS visibility attribute. When false, Visible when prevent any HTML from being rendered at all.

To change the css attribute, you will need to do it manually. You can do this by either removing the "error" class from the element (via the CssClass property) or by setting a style="visibility: visible" attribute manually via the Attributes property (since the style attribute overrides a css class):

control.Attributes["style"] = "visibility: visible";
Comments