6

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?

4

3 回答 3

15

Visible属性影响整个元素的渲染,与 CSS 可见性属性无关。如果为 false,则在完全阻止任何 HTML 呈现时可见。

要更改 css 属性,您需要手动进行。您可以通过从元素中删除“error”类(通过CssClass属性)或通过Attributes属性手动设置 style="visibility: visible" 属性(因为 style 属性覆盖 css 类)来做到这一点:

control.Attributes["style"] = "visibility: visible";
于 2009-07-12T10:25:22.090 回答
13

您对 CSS 可见性和控件的服务器端 Visible 属性感到困惑。为了更好地理解它,我建议您创建一个带有标签的示例页面,在 true 和 false 之间切换 Visible 属性并查看生成的 HTML。

你会发现如下。如真:

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

将渲染:

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

当设置为 false 时,它​​将呈现:

<div>

</div>
于 2009-07-12T10:29:07.277 回答
3

看看这个页面,它应该澄清一些事情:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.style.aspx

如前所述:

Visible 属性是服务器端的,它决定了服务器是否会呈现控件(如果没有呈现,则不会为其创建 HTML,并且它不在发送给客户端的最终 HTML 中)。

Style 属性控制元素的样式属性。该元素将被渲染,但您可以控制可见性 (CSS)。

于 2009-07-12T10:27:21.930 回答