You are on page 1of 3

7/4/2015

ASP.NETWebControl:TheRadioButtonList

WebControl:TheRadioButtonList

RadioButtonListFundamentals

Introduction
Toassistyouwithcreatingagroupofradiobuttons,youcanusetheRadioButtonListcontrol.
This control is implemented by the RadioButtonList class defined in the
System.Web.UI.WebControlsnamespaceoftheSystem.Web.dllassembly.

CreatingaRadioButtonList
Tovisuallycreatearadiobuttonlist,fromtheStandardsectionoftheToolbox,youcandrag
RadioButtonListanddropitontheform.Youwouldbeaskedtocreateeachitemofthegroup.
Todothis,youcanclickitsarrowbuttonandclickEditItems,thenusetheListItemCollection
Editor:

Iftheradiobuttonlistwasalreadycreated,toaccesstheListItemCollectionEditor,clickthe
radiobuttonlistontheform.InthePropertieswindow,clickItemsandclickbrowsebutton.
Tomanuallycreateagroupofradiobuttons,createan<asp:RadioButtonList>tagonthe
form:
<%@PageLanguage="C#"%>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h3>Exercise</h3>
<formid="frmRealEstate"method="post"runat="server">
<asp:RadioButtonListid="Genders"runat="server"></asp:RadioButtonList>
</form>
</body>
</html>

Tocreateeachitemofthegroup,
IfyouareusingtheListItemCollectionEditor,clickAdd

Ifyouareworkingmanually,betweentheopeningandtheclosingtags,createan
asp:ListItemtagwithitsownstartingandclosingtags:

<%@PageLanguage="C#"%>
<html>
<head>
<title>Exercise</title>
</head>

http://www.csharpkey.com/aspnet/controls/radiobuttonlist.htm

1/3

7/4/2015

ASP.NETWebControl:TheRadioButtonList

<body>

<formid="frmRealEstate"method="post"runat="server">
<asp:RadioButtonListid="Genders"runat="server">
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>

CharacteristicsofaRadioButtonList

TheCaptionsofRadioButtons
Obviouseachradiobuttonshoulddisplaytexttoshowwhatitisusedfor:
IfyouareworkingintheListItemCollectionEditor,ontheleftside,clickanitem.Onthe
rightside,clickTextandtypethedesiredstring
Ifyouareworkingmanually,tospecifythecaptionofaradiobutton,typeitsstring
betweenthebeginningandtheendingtags

Hereareexamples:

<%@PageLanguage="C#"%>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<formid="frmRealEstate"method="post"runat="server">
<asp:RadioButtonListid="Genders"runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>

Thiswouldproduce:

TheCaptionAlignmentofaRadioButton
By default, the captions of the radio buttons are positioned on the right side of their round
boxes. This characteristic is controlled by the TextAlign attribute of the RadioButtonList
controlandwhosedefaultvalueisRight.Ifyouwantthecaptionstobepositionedtotheleft,
assigntheLeftstringtothisattribute.Hereisanexample:
<%@PageLanguage="C#"%>
<html>
<head>
<title>Exercise</title>
</head>
<body>

http://www.csharpkey.com/aspnet/controls/radiobuttonlist.htm

2/3

7/4/2015

ASP.NETWebControl:TheRadioButtonList

<formid="frmRealEstate"method="post"runat="server">
<asp:RadioButtonListid="Genders"TextAlign="Left"runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
</asp:RadioButtonList>
</form>

</body>
</html>

Thiswouldproduce:

Home

Copyright20092011C#Key

http://www.csharpkey.com/aspnet/controls/radiobuttonlist.htm

3/3

You might also like