I would expect this:
<asp:CheckBox ID="CheckBox1" runat="server" CssClass="myClass" />
<asp:RadioButton ID="RadioButton1" runat="server" CssClass="myClass" />
<asp:TextBox ID="TextBox1" runat="server" CssClass="myClass" />
...to render like this (with some attributes removed for simplicity):
<input id="CheckBox1" type="checkbox" class="myClass" />
<input id="RadioButton1" type="radio" class="myClass" />
<input id="TextBox1" type="text" class="myClass" />
...when in fact, the RadioButton
and CheckBox
get wrapped with a span
tag and the CSS class gets applied there.
<span class="myClass"><input id="CheckBox1" type="checkbox" /></span>
<span class="myClass"><input id="RadioButton1" type="radio" /></span>
<input type="text" id="TextBox1" class="myClass" />
Is there a reason for this and is there a way to avoid it? It makes jQuery selectors ugly since you can't catch all of them with:
$("input.myClass")
Granted it is just going to:
$("input.myClass, span.myClass input")
...but that is ugly. I could write my own selector, but again not as elegant as it should be.
Every WebControl by default renders as a
<span>
tag, plus any custom rendering that the control author adds.One of the first things you usually do when you write a custom WebControl is to override the "TagKey" property to render a div, or something besides a span. The default value of this property is HtmlTextWriterTag.Span.
You could subclass your checkbox items and override the TagKey property to render something else, but then you have to deal with making all your checkboxes into your own version.
I came across this issue and am attempted to solve it using control adaptors.
See here for an example of doing this to a radio button list.
I ended up with this as my RadioButtonAdaptor-
And this added to my browsers file-
Of course, there are some downsides to this. Along with those mentioned at the above link, you also lose functionality if you do not impliment everything (the above code does not allow for a radio button to be checked). There are some CSS friendly control adaptors, but they do not cover the radio button control. It may be possible to use Reflector to get the default control adaptor as a starting point.
Web controls in the
System.Web.UI.WebControls
namespace may render differently in different browsers. You can't count on them rendering the same elements always. They may add anything that they think is needed to make it work in the specific browser.If you want to have any control over how the controls are rendered as html, you should use the controls in the
System.Web.UI.HtmlControls
namespace instead. That is:They will render just as the corresponding html element, with no extra elements added. This of course means that you will have to take responsibility for the browser compatibility, as the control doesn't. Also, those controls doesn't have all the features of the controls in the
WebControls
namespace.Found similar issue, wrote a simple jQuery based function to enable/disable checkbox and parent span
The plain RadioButton is often rendered without a span. If you set CssClass, Style, Enabled properties, the RadioButton is rendered with a span. This inconsistency is a real pain when I need to manipulate the radio button with client-side scripts. What I usually do is to apply a dummy CssClass so that it will always render a span consistently.
the best way i think is this:
to use it in custom control: