RadioButtonList Control in ASP.NET

RadioButtonList Control

RadioButtonList Control is same as DropDownList but it displays a list of radio buttons that can be arranged either horizontally or vertically. You can select only one item from the given RadioButtonList of options. These options are mutually exclusive.

The RadioButtonList control supports three important properties that affect its layout:

RepeatColumns: It displays the number of columns of radio buttons.
RepeatDirection: The direction that the radio buttons repeat. By default RepeatDirection value is vertical. Possible values are Horizontal and Vertical.
RepeatLayout: Determines whether the radio buttons display in an HTML table.

Possible values are as follows:

  • Table
  • Flow
  • OrderedList
  • UnorderedList

Example

using System;
using System.Web.UI.WebControls;
public partial class ListControls : System.Web.UI.Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {       
    }    
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "You have selected </br> Item=" +
                              RadioButtonList1.SelectedItem.Text + "</br> Value =" +
                              RadioButtonList1.SelectedValue       + "</br> Index =" +
                              RadioButtonList1.SelectedIndex ;                      
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (RadioButtonList1.RepeatDirection == RepeatDirection.Vertical)
        {
            RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
        }
        else
        {
            RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
        }
    }    
}


You can add items in RadioButtonList through item collection using property window.

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
      <asp:ListItem Value="1">Red</asp:ListItem>
      <asp:ListItem Value="2">Green</asp:ListItem>
      <asp:ListItem Value="3">Blue</asp:ListItem>
      <asp:ListItem Value="4">Yellow</asp:ListItem>
      <asp:ListItem Value="5">Orange</asp:ListItem>
</asp:RadioButtonList>


radiobuttonlist control

When you click on the RepeatDirection button the layout will be changed as Horizontal.

RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;

radiobuttonlist horizontal

By default RepeatColumns value is zero. You can change this value according to application need. If you set this value as three then output will be changed as:

radiobuttonlist horizontal