Click here to Skip to main content
13,148,503 members (54,770 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 10 Aug 2006

Enhanced RadioButtonList web control

, 22 Aug 2007
Rate this:
Please Sign up or sign in to vote.
The article describes how to enhance .NET's RadioButtonList web control to make the text wrap nicely on multiple lines in front of each radiobutton.


When you use .NET RadioButtonList web control you properbly know about it's left margin (wrapping) problem (see left picture below). This article will give you the source code to make a Web Custom Control that inherits from the RadioButtonList control to fix this problem.

Sample image

Here is the source code. You can either copy the code below or download the source file at the top of this article.

The only thing that happens when you use this Web Control, is when the radiobuttonlist has been rendered to the page a javascript will split the radiobutton and it's associated text into into two <td> tags instead of one.

using System; 
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.ComponentModel;
using System.Text; 

namespace Dfds.MCms.WebControls


/// <span class="code-SummaryComment"><summary> 
</span>/// This control inherit all the features from RadioButtonList. 
/// The enhanced feature makes the word wrapping correct, so if the text next to a radiobutton breaks into 
/// two lines, then line 2 will not be directly underneath the radiobutton, but an the text is correctly left aligned.
/// <span class="code-SummaryComment"></summary>

ToolboxData("<{0}:EnhancedRadioButtonList runat="server"></{0}:EnhancedRadioButtonList>")]

public class EnhancedRadioButtonList : System.Web.UI.WebControls.RadioButtonList

private string text;


public string Text 
return text;
text = value;

protected override void Render(HtmlTextWriter output)

StringBuilder sb = new StringBuilder();

<script language="'javascript'"> 

var s;
s = document.getElementById('{0}').outerHTML; 
s = s.replace(/<LABEL/gi,'</td><td><LABEL');
s = s.replace(/<td/gi,'<td valign=top'); 
document.getElementById('{0}').outerHTML = s;



Page.ClientScript.RegisterStartupScript(this.GetType(), "EnhancedRadioButtonList",sb.ToString());


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
ii_noname_ii7-Jul-10 2:07
memberii_noname_ii7-Jul-10 2:07 
GeneralUse CSS to generate the same result Pin
Barbara1124-Aug-07 11:52
memberBarbara1124-Aug-07 11:52 
GeneralRe: Use CSS to generate the same result Pin
codefranz19-Mar-08 10:17
membercodefranz19-Mar-08 10:17 
GeneralUse CSS to generate the same result Pin
Barbara1124-Aug-07 11:49
memberBarbara1124-Aug-07 11:49 
QuestionDoes it work for IE 7.0? Pin
sdanish7822-Aug-07 9:58
membersdanish7822-Aug-07 9:58 
AnswerRe: Does it work for IE 7.0? Pin
JacobEgholm22-Aug-07 10:27
memberJacobEgholm22-Aug-07 10:27 
GeneralRe: Does it work for IE 7.0? Pin
sdanish7823-Aug-07 6:50
membersdanish7823-Aug-07 6:50 
GeneralRe: Does it work for IE 7.0? Pin
JacobEgholm23-Aug-07 7:05
memberJacobEgholm23-Aug-07 7:05 
GeneralRe: Does it work for IE 7.0? Pin
sdanish7823-Aug-07 7:26
membersdanish7823-Aug-07 7:26 
Generalnot sure it works in Firefox... Pin
hawk_eye0116-Mar-07 11:13
memberhawk_eye0116-Mar-07 11:13 
GeneralRe: not sure it works in Firefox... Pin
ufriends23-Jul-07 8:31
memberufriends23-Jul-07 8:31 
Generalbetter javascript DOM support Pin
patt23-Aug-06 22:33
memberpatt23-Aug-06 22:33 
GeneralRe: better javascript DOM support [modified] Pin
JacobEgholm24-Aug-06 3:57
memberJacobEgholm24-Aug-06 3:57 
GeneralRe: better javascript DOM support Pin
p daddy24-Jan-07 5:37
memberp daddy24-Jan-07 5:37 
GeneralRe: better javascript DOM support Pin
JacobEgholm28-Jan-07 19:19
memberJacobEgholm28-Jan-07 19:19 
Generaldocument.all Pin
Alsvha18-Aug-06 4:50
memberAlsvha18-Aug-06 4:50 
GeneralRe: document.all Pin
JacobEgholm19-Aug-06 5:16
memberJacobEgholm19-Aug-06 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 22 Aug 2007
Article Copyright 2006 by JacobEgholm
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid