Friday, December 3, 2010

Difference between RadioButtonList and RadioButton

Difference between RadioButtonList and RadioButton
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”).
It is easy to bind this to a DataSource.
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" />

4 comments:

  1. radiobuttonlist will have a property called checked
    but radiobutton control will have both checked property and autopostback property

    ReplyDelete