10

I understand that we can set the various style attributes from code behind using :

divControl.Style("height") = "200px"

But how to set it's class instead of declaring each of the styles from code?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
wotney
  • 1,039
  • 3
  • 21
  • 34

2 Answers2

32

C#

divControl.Attributes["class"] = "myClass";

VB

divControl.Attributes("class") = "myClass"

You'll need to have a rule like this one on your css file

.myClass
{
   height:200px; 
   /*...more styles*/
}
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
3

This way you can define full style inline, without separate class declaration:

divControl.Attributes("style") = "height:200px; color:Red"
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • OP wants to set the `class` attribute of the div, not set styles individually. – Katie Kilian Jun 15 '12 at 20:41
  • 1
    Although the question specifically says that this is not the answer he is looking for, I gave it an up-vote because it helped me (google took me here when asking about the dynamic styles). – Goku Sep 27 '17 at 19:13
  • 1
    I found Yuriy's suggestion helpful for those situations where you need a one-time quick style fix dynamically. – Doreen Oct 12 '18 at 17:25