Search code examples
c#htmlcsshtml-tablehtmltable-control

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


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.


Solution

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