Giffyguy
Giffyguy

Reputation: 21282

How do I add CSS styles to an ASP.NET UserControl from the code-behind?

this.Style.Add ( /*...*/ )

... doesn't appear to work.

What do I need to do to add a style to it?

Upvotes: 1

Views: 9077

Answers (3)

Vitaliy Markitanov
Vitaliy Markitanov

Reputation: 2438

You may wrap your user control into another html container (div for example) and set style on container.

Upvotes: 2

Denis Ivin
Denis Ivin

Reputation: 5644

Think about the nature of UserControl - it's intended to contain other ASP.NET controls and that's why you cannot set CSS for whole control.. instead you just need to specify styles for each child control individually. You can also take a look at the ApplyStyleSheetSkin method.

Upvotes: 5

Erik Philips
Erik Philips

Reputation: 54618

If your user control is inheriting from webcontrol simply use the system.web.ui.webcontrols.webcontrol.cssclass (msdn). Example in the link.

Upvotes: 1

Related Questions