为什么在代码隐藏中将设置显示为none而不将其添加到HTML? [英] Why does setting display to none in the code-behind not add it to the HTML?
问题描述
我想设置几个HtmlTableRows的显示样式 显示:无,在后台代码如下所示:
I'm trying to set the display style of a couple of HtmlTableRows to "display:none" in code-behind like so:
foapalrow3 = new HtmlTableRow();
foapalrow3.ID = "foapalrow3";
foapalrow3.Attributes["display"] = "none";
...但它不起作用 - 查看源代码不包含display:nonefor无论是foapalrow3还是-4。为什么不,我怎么能强迫它按预期工作呢?
...but it's not working - the "View Source" contains no "display:none" for either foapalrow3 or -4. Why not, and how can I force this to work as intended?
要么我的麻木或墙壁最终会崩溃;我已经砰像后卫成砖墙与它,因为这流的-codedness 所示。
Either my nogging or the wall is going to eventually crumble with this; I've been slamming like a fullback into a brick wall with it, as this stream-of-codedness shows.
推荐答案
显示
不是HTML属性,因此会被丢弃。如果要添加CSS样式,请使用样式
而不是属性
,如下所示:
display
is not an HTML attribute, so it is discarded. If you want to add CSS styles, use Style
instead of Attributes
like this:
foapalrow3.Style["display"] = "none";
foapalrow4.Style.Add("display", "none"); // alternate syntax
正如另一个答案所述,理论上你可以用<$ c完成同样的事情$ c>属性[style] ,但我个人在过去遇到过这个问题, Style
属性是首选(在我看来,优越的选择。
As the other answer states, you could theoretically accomplish the same thing with Attributes["style"]
, but personally I've had issues with that in the past and the Style
property is the preferred (and in my opinion, superior) option.
这篇关于为什么在代码隐藏中将设置显示为none而不将其添加到HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!