Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
Fermin Avatar asked Jul 12 '09 10:07

Fermin


People also ask

Which CSS property controls visibility of the content that an element will be visible or hidden?

The visibility property specifies whether or not an element is visible. Tip: Hidden elements take up space on the page. Use the display property to both hide and remove an element from the document layout!

How do I use visibility hidden in CSS?

This property is used to specify whether an element is visible or not in a web document but the hidden elements take up space in the web document. Use the display property to remove or display property to both hide and delete an element from the browser.

What does visible false?

When you set Visible = false to a control, it is not rendered. That means there is no HTML representation of that control sent to the page.

What is the difference between visibility hidden and display none use an example to show difference?

display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). ... visibility:hidden means that unlike display:none , the tag is not visible, but space is allocated for it on the page. The tag is rendered, it just isn't seen on the page.


2 Answers

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";
like image 56
Richard Szalay Avatar answered Oct 13 '22 10:10

Richard Szalay


You are getting confused between CSS visibility and the control's server side Visible property. To understand it better I recommend you create a sample page with a label, toggle the Visible property between true and false and view the generated HTML.

What you will find is as follows. As true:

<div>
   <label runat="server" visible="true">Hello</label>
</div>

Will render:

<div>
    <label>Hello</label>
</div>

When set to false, it will render:

<div>

</div>
like image 36
RichardOD Avatar answered Oct 13 '22 11:10

RichardOD