Click here to Skip to main content
Click here to Skip to main content

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.

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.

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 Pinmemberii_noname_ii7-Jul-10 2:07 
GeneralUse CSS to generate the same result PinmemberBarbara1124-Aug-07 11:52 
GeneralRe: Use CSS to generate the same result Pinmembercodefranz19-Mar-08 10:17 
GeneralUse CSS to generate the same result PinmemberBarbara1124-Aug-07 11:49 
QuestionDoes it work for IE 7.0? Pinmembersdanish7822-Aug-07 9:58 
AnswerRe: Does it work for IE 7.0? PinmemberJacobEgholm22-Aug-07 10:27 
GeneralRe: Does it work for IE 7.0? Pinmembersdanish7823-Aug-07 6:50 
GeneralRe: Does it work for IE 7.0? PinmemberJacobEgholm23-Aug-07 7:05 
GeneralRe: Does it work for IE 7.0? Pinmembersdanish7823-Aug-07 7:26 
Generalnot sure it works in Firefox... Pinmemberhawk_eye0116-Mar-07 11:13 
GeneralRe: not sure it works in Firefox... Pinmemberufriends23-Jul-07 8:31 
Generalbetter javascript DOM support Pinmemberpatt23-Aug-06 22:33 
GeneralRe: better javascript DOM support [modified] PinmemberJacobEgholm24-Aug-06 3:57 
GeneralRe: better javascript DOM support Pinmemberp daddy24-Jan-07 5:37 
GeneralRe: better javascript DOM support PinmemberJacobEgholm28-Jan-07 19:19 
Generaldocument.all PinmemberAlsvha18-Aug-06 4:50 
GeneralRe: document.all PinmemberJacobEgholm19-Aug-06 5:16 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 22 Aug 2007
Article Copyright 2006 by JacobEgholm
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid