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
  • 96,782
  • 12
  • 124
  • 169
wotney
  • 999
  • 3
  • 19
  • 32

2 Answers2

31

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
  • 65,896
  • 14
  • 126
  • 152
3

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

divControl.Attributes("style") = "height:200px; color:Red"
Yuriy Galanter
  • 36,794
  • 12
  • 65
  • 122
  • 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