Search code examples
c#asp.netweb-controlshtml

How to remove span tag from WebControl when rendered


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?


Solution

  • Found this useful tip:

    In Code Behind, use InputAttributes instead of Attributes.

    For Example, type this:

    chk.InputAttributes.Add("onchange", "updateFields()")
    

    instead of this:

    chk.Attributes.Add("onchange", "updateFields()")
    

    or instead of inline declarations:

    <asp:CheckBox ID="chk" runat="server" onchange="updateFields()" />
    

    The last two will cause the wrapping span.