2

Possible Duplicate:
Adding css class through aspx code behind

Im trying to assign a css-class to button created in a overridden CreateChildControls-method like this:

HtmlButton btn = new HtmlButton();
btn.ServerClick += new EventHandler(resultSelected);
btn.InnerText = "btn text";
btn.Attributes.CssStyle.Remove("class");
btn.Attributes.CssStyle.Add("class", "submitbutton");
this.Controls.Add(btn);
PlaceHolder1.Controls.Add(btn);

But I guess I need to specify my stylesheet-file for it to work...I want to use the same one as in the aspx.

Community
  • 1
  • 1
mdc
  • 1,161
  • 6
  • 22
  • 37
  • Have a look [Adding css class through aspx code behind](http://stackoverflow.com/questions/1903513/adding-css-class-through-aspx-code-behind) – huMpty duMpty Nov 01 '12 at 11:51

3 Answers3

7

Following code will work,

btn.Attributes["class"]= "submitbutton";

OR

btn.CssClass="submitbutton";
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
1

You need to remove CssStyle

btn.Attributes.Remove("class");
btn.Attributes.Add("class", "submitbutton");

Or

btn.CssClass="submitbutton";
Priyank Patel
  • 6,898
  • 11
  • 58
  • 88
1

You are using the CssStyle property incorrectly. This property expects you to provide CSS name value pairs. What you want to do is add an Attribute with name "class".

btn.Attributes.Add("class", "submitbutton");

Have a look at the MSDN for CssStyle property. It is what is rendered into the inline style attribute.

Bazzz
  • 26,427
  • 12
  • 52
  • 69