Click here to Skip to main content
15,347,625 members
Articles / Web Development / ASP.NET
Article
Posted 10 Aug 2006

Stats

43K views
221 downloads
18 bookmarked

Enhanced RadioButtonList web control

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

{

/// <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>
</span>
 

[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

Share

About the Author

JacobEgholm
Web Developer
Denmark Denmark
No Biography provided

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 
Thanks for this control, I have one quick question:

Is the Text property and variable used at all, or was it created by a wizard?

<br />
<br />
private string text;<br />
<br />
[Bindable(true), <br />
Category("Appearance"), <br />
DefaultValue("")] <br />
<br />
public string Text <br />
{<br />
   get<br />
   {<br />
      return text;<br />
   }<br />
   set<br />
   {<br />
      text = value;<br />
   }<br />
}<br />
<br />


This all seems superfluous to me, or am I missing something!?

Thanks again for the control.


PB
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.