Search code examples
c#htmlcontrols

Is it possible to set a class attribute from a System.Web.UI.HtmlControls.HtmlButton though code?


I am looking for a way to add class attributes to a button so it can be styled, but I can't figure out how to do this from my code.

The System.Web.UI.HtmlControls.HtmlButton has a AttributeCollection property but it's read-only, so that's no use to me.

System.Web.UI.WebControls.Button has a CssClass property, I need something like this for the HtmlButton. I can't use the button provided from the WebControls because I need to use the InnerHtml property of the HtmlButton to place a icon on the button, but I do want to be able to style it as well!

Does anyone have a solution for this?


Solution

  • You should be able to set the css class like this

    theHtmlButton.Attributes["class"] = "yourClass";
    

    You can use this method to set any attribute, including your own custom attributes.