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?

0

2 Answers 2

33

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*/
}
Sign up to request clarification or add additional context in comments.

2 Comments

Write that in VB.NET instead of C# and it'll be perfect.
@Andrew Morton: he states on the question that VB or C# is fine but thanks for the suggestion, added VB code.
3

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

divControl.Attributes("style") = "height:200px; color:Red"

3 Comments

OP wants to set the class attribute of the div, not set styles individually.
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).
I found Yuriy's suggestion helpful for those situations where you need a one-time quick style fix dynamically.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.