具有 CSS 可见性的 ASP.NET 控件:隐藏,未在 Control.Visible = true 上显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:46:55  来源:igfitidea点击:

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

asp.netcssvisibility

提问by Fermin

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

我的页面上有一些带有“错误”类的标签,.error 的规则是:

.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 = Truewhen 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?

然后我在后面的代码中设置错误标签的 .Text 属性。
如果我lblError.Visible = True在设置文本时使用,则不会显示标签。任何想法为什么会这样?我可能在这里错了,但我认为设置 .Visible 就像设置可见性样式?

回答by Richard Szalay

The Visibleproperty 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.

可见属性影响呈现元件整体,是无关的CSS visibility属性。如果为 false,则在完全阻止任何 HTML 呈现时可见。

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 CssClassproperty) or by setting a style="visibility: visible" attribute manually via the Attributesproperty (since the style attribute overrides a css class):

要更改 css 属性,您需要手动进行。您可以通过从元素中删除“错误”类(通过CssClass属性)或通过Attributes属性手动设置 style="visibility:visible" 属性(因为 style 属性覆盖了 css 类)来实现此目的:

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

回答by RichardOD

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.

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

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

<div>

</div>

回答by Zyphrax

Have a look at this page, it should clarify things: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.style.aspx

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

As written before:

就像之前写的:

The Visible property is serverside and determines if the Server will render the control or not (if it's not rendered, no HTML will be created for it, and it's not in the final HTML send to the client).

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

The Style property controls the style attribute of the element. The element will be rendered but you can control the visibility (CSS).

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