Polecat Harris
Polecat Harris

Reputation: 177

Creating a CSS class programmatically ASP.Net

I have a DIV that I would like to modify the CSS for programmatically in VB.Net/C#.

I know that, for example, I could add style attributes simply by

divMyDiv.Style.add("color","#ff0000")

but I want to add a new CSS class, together with its attributes to the DIV. So in an ideal world I would like to write something like

divMyDiv.Style.add(".smallRedText", "{font-size:10px; color:#ff0000}")

Is this even possible ? Am I missing the bigger picture ?

All help gratefully received :-)

Upvotes: 0

Views: 924

Answers (2)

Justin Skiles
Justin Skiles

Reputation: 9523

I suggest that you define the variations in css ahead of time that you know you'll need. Then, you can manipulate the class attribute of whatever control you need to change on the fly.

For example, you could define:

.smallRedText { font-size: 10px; color: #ff0000 }
.smallBlueText { font-size: 10px; color: #0000ff }

Then when comes time to select dynamically:

divMyDiv.Attributes.Add("class", whateverClassWeNeed);

Upvotes: 0

Joe Ratzer
Joe Ratzer

Reputation: 18569

What about:

divMyDiv.Attributes.Add("class", "new-class")

Upvotes: 1

Related Questions