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
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.
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
should do
I just had this issue and used Jon's answer, which is good and it works. The downside is that your class is defined within the codebehind and not your markup.
So I took the answer and made a progamatic way to retrieve all attributes for the control, copy them to InputAttributes and remove those copied attributes from attributes.
Note that while this extends from RadioButton, you could use the method to extend any control, such as labels or checkboxes.
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Hidistro.UI.Common.Controls
{
/// <summary>
/// Just like a normal RadioButton, except that the wrapped span is disabled.
/// </summary>
public class CleanRadioButton : RadioButton
{
protected override void Render(HtmlTextWriter writer)
{
List<string> keysToRemove = new List<string>();
foreach (object key in Attributes.Keys)
{
string keyString = (string)key;
InputAttributes.Add(keyString, Attributes[keyString]);
keysToRemove.Add(keyString);
}
foreach (string key in keysToRemove)
Attributes.Remove(key);
base.Render(writer);
}
}
}
This way, all you need to do is the following, and it will output tags without the span.
<namespace:CleanRadioButton class="class1" />
<namespace:CleanRadioButton class="class2" />
HTML output: (note that "generated" is autogenerated)
<input id="generated" type="radio" name="generated" value="generated" class="class1">
<input id="generated" type="radio" name="generated" value="generated" class="class2">
i wonder, is there a reason no1 mentioned this:
public MyControl() : base(string.Empty)
i got this reading http://aspnetresources.com/blog/stripping_span_from_webcontrol that flaviotsf mentioned
<div class="onoffswitch">
<asp:CheckBox ID="example1" runat="server" AutoPostBack="true" OnCheckedChanged="chkWifiRequired_CheckedChanged" CssClass="aspNetDisabled onoffswitch-checkbox"/>
<label class="onoffswitch-label" for='<%= example1.ClientID.ToString() %>'>
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
/* remove the relative spam involving inputs disabled */ $('input[name=""]').parent('.aspNetDisabled').each(function () { var $this = $(this); var cssClass = "onoffswitch-checkbox"; $('input[name=""]').addClass(cssClass).unwrap().parent('label[for],span').first().addClass('onoffswitch-checkbox'); });
This will allow you to use the check box normally, still have it call server side code, and use the toggle from bootstrap. (I'm using the Inspina theme but it should be the same format for other toggles)
I don't know if this will work on this particular example. But if you make your own WebControl and you never want it to render spans around itself you can override the Controls render method like this:
public class MyWebControl : WebControl
{
/* Your code
between here */
protected override void Render(HtmlTextWriter writer)
{
RenderContents (writer);
}
}
I guess that you could add the same to an inherited CheckBox... something like this:
public class MyCheckBox : CheckBox
{
/* Your code
between here */
protected override void Render(HtmlTextWriter writer)
{
RenderContents (writer);
}
}
The basic problem with the solution is better explained here:
http://www.marten-online.com/net/stripping-span-tag-from-webcontrol.html