Click here to Skip to main content
Licence 
First Posted 10 Aug 2006
Views 29,198
Downloads 134
Bookmarked 18 times

Enhanced RadioButtonList web control

By | 22 Aug 2007 | Article
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

{

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

About the Author

JacobEgholm

Web Developer

Denmark Denmark

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberii_noname_ii2:07 7 Jul '10  
GeneralUse CSS to generate the same result PinmemberBarbara1111:52 24 Aug '07  
GeneralRe: Use CSS to generate the same result Pinmembercodefranz10:17 19 Mar '08  
GeneralUse CSS to generate the same result PinmemberBarbara1111:49 24 Aug '07  
QuestionDoes it work for IE 7.0? Pinmembersdanish789:58 22 Aug '07  
AnswerRe: Does it work for IE 7.0? PinmemberJacobEgholm10:27 22 Aug '07  
GeneralRe: Does it work for IE 7.0? Pinmembersdanish786:50 23 Aug '07  
GeneralRe: Does it work for IE 7.0? PinmemberJacobEgholm7:05 23 Aug '07  
GeneralRe: Does it work for IE 7.0? Pinmembersdanish787:26 23 Aug '07  
Generalnot sure it works in Firefox... Pinmemberhawk_eye0111:13 16 Mar '07  
GeneralRe: not sure it works in Firefox... Pinmemberufriends8:31 23 Jul '07  
Generalbetter javascript DOM support Pinmemberpatt22:33 23 Aug '06  
GeneralRe: better javascript DOM support [modified] PinmemberJacobEgholm3:57 24 Aug '06  
GeneralRe: better javascript DOM support Pinmemberp daddy5:37 24 Jan '07  
GeneralRe: better javascript DOM support PinmemberJacobEgholm19:19 28 Jan '07  
Generaldocument.all PinmemberAlsvha4:50 18 Aug '06  
GeneralRe: document.all PinmemberJacobEgholm5:16 19 Aug '06  

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.

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