Why does setting display to none in the code-behind not add it to the HTML?

时光怂恿深爱的人放手 提交于 2019-12-24 14:17:50

问题


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";

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

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.


回答1:


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

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.




回答2:


display is not the name of the attribute. You have to modify the style attribute.

foapalrow3.Attributes["style"] = "display:none";


来源:https://stackoverflow.com/questions/31300557/why-does-setting-display-to-none-in-the-code-behind-not-add-it-to-the-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!