Search code examples
asp.netmarkup

controlling how ASP.NET renders radio button lists in HTML


When I add ListItems to a RadioButtonList in code-behind, the labels are rendered after the input and placed inside a TD by ASP.NET.

     <td>
         <input type='radio' id='site123' name='groupname' value='123' />
         <label for = 'site123'>this is a long label that wraps</label>
     </td>

Is there a way to control how ASP.NET renders the markup, either selectively or globally? I'd like to have the input inside the label:

       <label><input .... /> </label>

Solution

  • You can use a Control Adapter. They allow you to override the html rendering of any ASP.Net control. Aside from the link, I have a simple example in another question here:

    Dropdownlist control with <optgroup>s for asp.net (webforms)?

    Since you asked about doing this selectively, you would need to inherit a control from RadioButtonList, and then attach your control adapter to the inherited control, instead of RadioButtonList itself.