views:

883

answers:

4

When using an asp.net CheckBox (and in out case, inherited from a CheckBox) it renders a span around the checkbox input control, this span control is affecting jQuery scripts.

Is it possible to remove this span when rendering?

A: 

You can use the input/checkbox control directly if you don't need a label, or can put one yourself:

<input type="checkbox" id="CheckBox1" runat="server" />
<label for="CheckBox1">My Label</label>

A CSS Adapter may be able to remove the span around the checkbox/label, but I haven't seen one for that purpose.

Nick Craver
I've had to use this approach when the <span> tags were getting in my way.
egrunin
I could use the <input> tag tag directly, however our derived CheckBox includes a Command Event which may be used. Ideally we would like to solve this within the WebControl.
Mark Redman
@Mark - If that's not an option, you could inherit from Checkbox, over-write the rendering, or call it's `SetRenderMethodDelegate()` method. Rick Strahl has a rundown of it here: http://www.west-wind.com/Weblog/posts/6138.aspx
Nick Craver
A: 

Why don't you remove the span using .remove with jquery ?

Yassir
Because the `<span />` wraps the `<input />` tag, which would be very inefficient to do correctly.
Dan Herbert
Although I like jQuery a lot and it could solve the problem, I would prefer solving the problem at a lower level which will be more efficient.
Mark Redman
+1  A: 

I just tried this on a test page and I'm not getting the around my CheckBox controls... are you sure it's the CheckBox that's rendering this? Is it conditional?

UPDATE: OK, it appears to be conditional on whether or not the CheckBox has extra attributes, including a CssClass setting... or a "disabled" attribute.

Bryan
Yes, it appears that all styles are added to a span wrapping the checkbox input control. I can seem to get rid of the span if I inherit from Control rather than checkbox, although I have to re-implement pretty much every property, eg an OnClientClick property that renders as onclick. I am using additional (non .net or custom attributes which as you mention may also be the cause) I would think that the ability to inherit from an asp.net CheckBox and elimiate the span would be easy, but its appears not.
Mark Redman
I would take Nick's suggestion then and override the Render method. You could take a shortcut by using the original CheckBox render method (cut and paste from Reflector) and just pull out the span tags.
Bryan
A: 

Can you use a literal control instead? There's a big difference between these two alternatives:

<p>12345<asp:Label ID="lblMiddle" runat="server" Text="6"></asp:Label>7890</p>
<p>12345<asp:Literal ID="ltlMiddle" runat="server" Text="6"></asp:Literal>7890</p>
Bradbox