1

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.

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • The output will contain the *HTML attribute* 'display' - ie. `` - which has naught to do with CSS. – user2864740 Jul 08 '15 at 18:26
  • Actually, the rows are not decorated with anything; in fact, they're not even in the HTML (the hidden ones are not). – B. Clay Shannon-B. Crow Raven Jul 08 '15 at 18:40
  • Setting `Visible = false` *disables* control rendering server-side. *If* the control *was* rendered it *would* have the attribute. Rendering-to-HTML, or not, is independent of the Attribute collection and hence the code shown; switching to use the Style collection or style attribute will not change this. – user2864740 Jul 08 '15 at 18:40
  • Yeah, I just commented out the visible jazz and it's working pretty sweetly. – B. Clay Shannon-B. Crow Raven Jul 08 '15 at 18:46

2 Answers2

3

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.

Sabre
  • 359
  • 2
  • 13
2

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

foapalrow3.Attributes["style"] = "display:none";
Seth
  • 706
  • 4
  • 20
  • Thanks; I reckon your way is probably right, too, but I marked the answer correct for the cat with fewer points (I always favor the underdog - unless the underdog is a jerk, that is). – B. Clay Shannon-B. Crow Raven Jul 08 '15 at 18:45