Fermin Fermin - 1 year ago 71
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 {

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 Source

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";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download