Slide 12.6: Radio buttons
Slide 12.8: Hidden fields
Home

Radio Button Lists


The RadioButtonList control provides a single-selection checked list. Like other list controls, RadioButtonList has an Items collection with members that correspond to each item in the list. To determine which items are selected, test the Selected property of each item. You can control the rendering of the list with the RepeatLayout and RepeatDirection properties: By default, the value of RepeatDirection is Vertical. Setting this property to Horizontal causes the list to be rendered horizontally. The following sample illustrates using the RadioButtonList control.


RadioButtonList1_cs.aspx

Web
<%@ Page Language="C#" %>
<script language="C#" runat="server">
 protected void Button1_Click( object sender, EventArgs e ) {
  if ( RadioButtonList1.SelectedIndex > -1 )
   Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
 }
 protected void chkLayout_CheckedChanged( object sender, EventArgs e ) {
  if ( chkLayout.Checked )
   RadioButtonList1.RepeatLayout = RepeatLayout.Table;
  else
   RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
 }
 protected void chkDirection_CheckedChanged( object sender, EventArgs e ) {
  if ( chkDirection.Checked )
   RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
  else
   RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
 }
</script>

<html> 
 <body> 
  <form runat="server">
   <asp:RadioButtonList id="RadioButtonList1" runat="server">
    <asp:ListItem>Item 1</asp:ListItem>
    <asp:ListItem>Item 2</asp:ListItem>
    <asp:ListItem>Item 3</asp:ListItem>
    <asp:ListItem>Item 4</asp:ListItem>
    <asp:ListItem>Item 5</asp:ListItem>
    <asp:ListItem>Item 6</asp:ListItem>
   </asp:RadioButtonList>
        
   <asp:CheckBox id="chkLayout"
     OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout"
     Checked=true AutoPostBack="true" runat="server" />
   <asp:CheckBox id="chkDirection"
     OnCheckedChanged="chkDirection_CheckedChanged"
     Text="Display Horizontally" AutoPostBack="true" runat="server" />
   <asp:Button id="Button1" Text="Submit" onclick="Button1_Click"
     runat="server" />
   <asp:Label id="Label1" Font-Names="Verdana" font-size="8pt"
     runat="server" />
  </form>        
 </body>
</html>