Press enter to see results or esc to cancel.

ASP.NET renders CheckBox control inside span if CssClass is set

I added an ASP.NET CheckBox control to a form I was creating and was surprised to see the following unexpected html rendered when CssClass property was set for the control:

ASP.NET code:

<asp:CheckBox runat="server" ID="CheckBox1" CssClass="required" />

Rendered HTML:

<span class="required">
<input id="ctl00_CheckBox1" type="checkbox" />

I tried adding the class attribute to the control’s Attributes collection in code-behind without success. Finally, after reading StackOverflow and MSDN, I found out that you can indeed control what is output by adding the class attribute to the the control’s InputAttributes collection as follows:

CheckBox1.InputAttributes["class"] = "required";

Leave a Comment