Web Control: The Radio Button List
To assist you with creating a group of radio buttons, you can use the RadioButtonList control. This control is implemented by the RadioButtonList class…
ASP.NET Web Control: The Radio Button List
Web Control: The Radio Button List#
Radio Button List Fundamentals#
Introduction#
The RadioButtonList control is available to help you manage groups of radio buttons more efficiently. This control belongs to the RadioButtonList class, which is located in the System.Web.UI.WebControls namespace within the System.Web.dll assembly.
Creating a Radio Button List#
To visually create a radio button list, from the Standard section of the Toolbox, you can drag RadioButtonList and drop it on the form. You would be asked to create each item of the group. To do this, you can click its arrow button and click Edit Items, then use the ListItem Collection Editor:
If the radio button list was already created, to access the ListItem Collection Editor, click the radio button list on the form. In the Properties window, click Items and click browse button. To manually create a group of radio buttons, create an <asp:RadioButtonList> tag on the form:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<h3>Exercise</h3>
<form id="frmRealEstate" method="post" runat="server">
<asp:RadioButtonList id="Genders" runat="server"></asp:RadioButtonList>
</form>
</body>
</html>To create each item of the group,
- If you are using the ListItem Collection Editor, click Add
- If you are working manually, between the opening and the closing tags, create an asp:ListItem tag with its own starting and closing tags:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:RadioButtonList id="Genders" runat="server">
<asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>Characteristics of a Radio Button List#
The Captions of Radio Buttons#
Clearly, every radio button needs to show some descriptive text so that users understand its purpose:
- If you are working in the ListItem Collection Editor, on the left side, click an item. On the right side, click Text and type the desired string
- If you are working manually, to specify the caption of a radio button, type its string between the beginning and the ending tags
Here are examples:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:RadioButtonList id="Genders" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
</asp:RadioButtonList>
</form>
</body>
</html>This would produce:
The Caption Alignment of a Radio Button#
Normally, the labels for radio buttons appear on the right side of their circular selectors. This behavior is defined by the RadioButtonList control's TextAlign property, which defaults to Right. To move the labels to the left side, you can set this property to the string Left. For instance:
<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<form id="frmRealEstate" method="post" runat="server">
<asp:RadioButtonList id="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>This would produce:
Home Copyright © 2009 C# Key

