tags:

views:

2780

answers:

2

Currently I am trying to modify a css style attribute for a div based on the information I get from a database table in the code behind of my aspx page. In simplified form the following is essentially what I am trying to do but I get errors.

Here is my code:

ASPX:

<div id="testSpace" runat="server">
Test
</div>

CODE BEHIND:

testSpace.Style = "display:none;"

testSpace.Style("display") = "none";

Any help would be appreciated. Thanks!

+4  A: 

I don't know if this is the "right" way to do it, but it seems to work:

testSpace.Style.Add("display", "none");
Andy White
testSpace.Attributes.Add("style", "display: none;"); would also work.
Robert C. Barth
That's actually what I thought to try first, but I fell back to the "Style" property since it seems like that's what it's there for. :)
Andy White
+2  A: 

It's an HtmlGenericControl so not sure what the recommended way to do this is, so you could also do:

testSpace.Attributes.Add("style", "text-align: center;");

or

testSpace.Attributes.Add("class", "centerIt");

or

testSpace.Attributes["style"] = "text-align: center;";

or

testSpace.Attributes["class"] = "centerIt";

Hope that helps, Nick

nickyt
The attributes way is the way I have always done it. It works...
This is also correct - but Andy answered first so he gets it. If I could do more than one answer i'd check both!
Roy