Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Article

Enhanced RadioButtonList web control

Rate me:
Please Sign up or sign in to vote.
1.50/5 (6 votes)
22 Aug 2007 44.4K   221   18   17
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.

Introduction

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.

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

namespace Dfds.MCms.WebControls

{

/// <summary> 
/// 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.
/// </summary>

 

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

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

private string text;

<Bindable(true), 
Category("Appearance"), 
DefaultValue("")> 

public string Text 
{
get
{
return text;
}
set
{
text = value;
}
} 

protected override void Render(HtmlTextWriter output)
{

StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"

<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;

</script>

",this.ClientID);


Page.ClientScript.RegisterStartupScript(this.GetType(), "EnhancedRadioButtonList",sb.ToString());
base.Render(output);
}
}
}

License

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


Written By
Web Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
ii_noname_ii7-Jul-10 2:07
ii_noname_ii7-Jul-10 2:07 
GeneralUse CSS to generate the same result Pin
Barbara1124-Aug-07 11:52
Barbara1124-Aug-07 11:52 
GeneralRe: Use CSS to generate the same result Pin
codefranz19-Mar-08 10:17
codefranz19-Mar-08 10:17 
GeneralUse CSS to generate the same result Pin
Barbara1124-Aug-07 11:49
Barbara1124-Aug-07 11:49 
QuestionDoes it work for IE 7.0? Pin
sdanish7822-Aug-07 9:58
sdanish7822-Aug-07 9:58 
AnswerRe: Does it work for IE 7.0? Pin
JacobEgholm22-Aug-07 10:27
JacobEgholm22-Aug-07 10:27 
GeneralRe: Does it work for IE 7.0? Pin
sdanish7823-Aug-07 6:50
sdanish7823-Aug-07 6:50 
GeneralRe: Does it work for IE 7.0? Pin
JacobEgholm23-Aug-07 7:05
JacobEgholm23-Aug-07 7:05 
GeneralRe: Does it work for IE 7.0? Pin
sdanish7823-Aug-07 7:26
sdanish7823-Aug-07 7:26 
Generalnot sure it works in Firefox... Pin
hawk_eye0116-Mar-07 11:13
hawk_eye0116-Mar-07 11:13 
GeneralRe: not sure it works in Firefox... Pin
ufriends23-Jul-07 8:31
ufriends23-Jul-07 8:31 
Generalbetter javascript DOM support Pin
patt23-Aug-06 22:33
patt23-Aug-06 22:33 
GeneralRe: better javascript DOM support [modified] Pin
JacobEgholm24-Aug-06 3:57
JacobEgholm24-Aug-06 3:57 
GeneralRe: better javascript DOM support Pin
p daddy24-Jan-07 5:37
p daddy24-Jan-07 5:37 
GeneralRe: better javascript DOM support Pin
JacobEgholm28-Jan-07 19:19
JacobEgholm28-Jan-07 19:19 
Thanks for your message.
Yes, you can just delete the Text property. It was created by the wizard and I forgot to remove itBlush | :O )

Jacob Egholm

Generaldocument.all Pin
Alsvha18-Aug-06 4:50
Alsvha18-Aug-06 4:50 
GeneralRe: document.all Pin
JacobEgholm19-Aug-06 5:16
JacobEgholm19-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.