markzzz
markzzz

Reputation: 47945

Can I overwrite a css class in a UserControl?

I have this context:

<asp:Panel ID="DescrizionePagina" CssClass="pagina-testo-box-large" runat="server">Text</asp:Panel>

and I'd like to overwrite the class with pagina-testo-box-small:

Panel myPanel = (Panel)this.Parent.FindControl("DescrizionePagina");
myPanel.Attributes.Add("class", "pagina-testo-box-small");

but it doesnt works... pagina-testo-box-large remains...

Upvotes: 4

Views: 1594

Answers (4)

शेखर
शेखर

Reputation: 17614

If this doesn't work as suggest in the above answers

Panel myPanel = (Panel)this.Parent.FindControl("DescrizionePagina");
myPanel.CssClass = "pagina-testo-box-small";

Then you can use like this for individual attribute use !important

 Panel myPanel = (Panel)this.Parent.FindControl("DescrizionePagina");
 myPanel.Style.Add("float", "left!important");

Upvotes: 0

Stefano Altieri
Stefano Altieri

Reputation: 4628

Use the following code:

myPanel.CssClass = "pagina-testo-box-small";

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460038

So if you already know the property, why don't you use it?

Panel myPanel = (Panel)this.Parent.FindControl("DescrizionePagina");
myPanel.CssClass = "pagina-testo-box-small";

Upvotes: 2

Heinzi
Heinzi

Reputation: 172210

Simply use the CssClass property -- the same that you already set in your ASPX code:

myPanel.CssClass = "pagina-testo-box-small";

Behind the scenes, the .NET property CssClass will be translated to the HTML class attribute, but ASP.NET takes care of this automatically.

Upvotes: 0

Related Questions