Lawrance
Lawrance

Reputation: 1055

What is the difference between the RadioButon and RadioButtonList?

I need to know the difference between a RadioButton and a RadioButtonList, and what are the guidelines to use when deciding which one to use?

I researched this and decided to post my findings here to help illustrate the differences I found that should help clarify my question:

What I Learned:

RadioButton

Used to display a single RadioButton at a time. Likely requires setting group attribute to associate multiple RadioButton controls into a group.

RadioButtonList

Used to Display a group of RadioButton controls, automatically providing the group attribute associating all the included RadioButton controls into a single group.

Observation

Visually, both produce the same result in the UI, provided one places at least 2 or more RadioButton controls on the page with the same value for the group attribute.

UI Sample Code follows

asp:RadioButton

<asp:RadioButton ID="b2b" text="B to B" checked="true" runat ="server" GroupName="businesstype" />
<asp:RadioButton ID="b2c" text="B to C" runat ="server" GroupName="businesstype" />

asp:RadioButtonList

<asp:RadioButtonList ID="businesstype" runat="server" >
    <asp:ListItem Selected="True" Value="0">B to B</asp:ListItem>
    <asp:ListItem Value="1">B to C</asp:ListItem>
</asp:RadioButtonList>

What are the guidelines for the use of each?

Upvotes: 3

Views: 17834

Answers (3)

Varun Pozhath
Varun Pozhath

Reputation: 626

An asp:radiobuttonlist creates a group of radiobuttons that ensures when one is selected, the others are deselected whereas asp:radiobutton is not within a group and therefore cannot be deselected by clicking other radio buttons.

Upvotes: 0

Shiji kumaran
Shiji kumaran

Reputation: 31

1. RadioButtonList

RadioButtonList is a single control with a list of RadioButtons. This is derived from ListControl class. So this will work similar to other list controls like ListBox, DropDownList and CheckBoxList. For giving a caption for buttons you can use the Text property. You cannot insert a text in between two buttons. Using the “SelectedIndexChanged” event you will get the selected buttons value (“RadioButtonList1.SelectedValue”).

for e.g

private void Bind()
{
  RadioButtonList1.DataSource = dsEmployees;
  RadioButtonList1.DataTextField = "EmployeeName";
  RadioButtonList1.DataValueField = "EmployeeID";
  RadioButtonList1.DataBind();
} 

If you are using HTML

<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
  RepeatDirection="Horizontal" 
  onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
  <asp:ListItem Text="Male" Value="1" ></asp:ListItem>
  <asp:ListItem Text="Female" Value="2" ></asp:ListItem>
</asp:RadioButtonList>

2. RadioButton

RadioButton” is a single control, it is derived from “CheckBox” Class. You have to set the GroupName property to identify a group. Also the event handler for the event “CheckedChanged” will help us to do some job. Another one thing is you have to write separate handlers for each radio button.

For e.g.:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" 
   AutoPostBack="true" oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" />
   <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" 
 AutoPostBack="true" oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" />

Upvotes: 3

शेखर
शेखर

Reputation: 17614

You can get the selected Index in RadioButtonList as it work on collection of ListItem.

You can visit here for more detail

In contrast, the RadioButtonList control is a single control that acts as a parent control for a collection of radio button list items.

It derives from a base ListControl Class, and therefore works much like the ListBox, DropDownList, and CheckBoxList Web server controls. Therefore, many of the procedures for working with the RadioButtonList control are the same as they are for the other list Web server controls.

Upvotes: 0

Related Questions