Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ASP.Net RadioButton and CheckBox render inside a Span?

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.

like image 856
Mark Avatar asked Jun 15 '09 17:06

Mark


People also ask

What is the purpose of ASP NET radio button control?

ASP.NET Web Forms RadioButton. It is an input control which is used to takes input from the user. It allows user to select a choice from the group of choices. To create RadioButton we can drag it from the toolbox of visual studio.

What is the use of checkbox in asp net?

It is used to get multiple inputs from the user. It allows user to select choices from the set of choices. It takes user input in yes or no format. It is useful when we want multiple choices from the user.


2 Answers

This was driving me crazy too until I found the inputAttributes property.

For example, here is how to add a class directly on the checkbox control without the span nonsense:

myCheckBoxControl.InputAttributes.Add("class", "myCheckBoxClass") 
like image 112
JohnFx Avatar answered Oct 06 '22 07:10

JohnFx


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:

<input type="checkbox" id="CheckBox1" runat="server" class="myClass" /> <input type="radio" name="RadioButton1" runat="server" class="myClass" /> <input type="text" id="TextBox1" runat="server" class="myClass" /> 

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.

like image 39
Guffa Avatar answered Oct 06 '22 07:10

Guffa