asp.net RadioButtonList control
RadioButtonList is an asp.net server control used to display list of radiobuttons and they grouped by default. This control is also used to allow only one selection in list of items.
Design code
<asp:RadioButtonList ID=”RadioButtonList1″ runat=”server” RepeatColumns=”2″>
<asp:ListItem>Apples</asp:ListItem>
<asp:ListItem>Grapes</asp:ListItem>
<asp:ListItem>Bananas</asp:ListItem>
<asp:ListItem>Mangos</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Cocoa</asp:ListItem>
<asp:ListItem>CustardApple</asp:ListItem>
<asp:ListItem>Breadfruit</asp:ListItem>
</asp:RadioButtonList>
Properties of RadioButtonList
Cellpadding: The padding between each item.
CellSpacing: The spacing between each item.
RepeatColumns: The number of columns to use to lay out the items.
RepeatColumns=2
Repeat Direction: The direction in which items are laid out.
Horizontal / Vertical
Items: The collection of items in the list
TextAlign: The alignment of the text label with respect to each item.
Getting RadioButtonList Selected Item into label control
Step1: Drag RadioButtonList control and add above fruits list items
Step2: Add one Button control and Label control
Design code:
<asp:Label id=”Label1″ runat=”server”/>
<br/>
<asp:RadioButtonList ID=”RadioButtonList1″ runat=”server” RepeatColumns=”2″>
<asp:ListItem>Apples</asp:ListItem>
<asp:ListItem>Grapes</asp:ListItem>
<asp:ListItem>Bananas</asp:ListItem>
<asp:ListItem>Mangos</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Oranges</asp:ListItem>
<asp:ListItem>Cocoa</asp:ListItem>
<asp:ListItem>CustardApple</asp:ListItem>
<asp:ListItem>Breadfruit</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button” />
Code: Double click Button control then write below code
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = “Your favorite fruits is ” + RadioButtonList1.SelectedItem.Text;
}
RadioButtonList SelectedIndexChanged event
Step1: Use above same design code remove Button.
Step2: keep RadioButtonList AutopostBack Property is True
Step3: Double click in RadioButtonList then selectedIndexedChanded event fires
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = “RadioButtonList selectedIndexChanged items is ” + RadioButtonList1.SelectedItem.Text;
}
using above code selected list item will display in Label control,dont forget to keep RadioButtonList property autopostBack True using RadioButtonList1_SelectedIndexChanged
How to edit selected value in Radiobuttonlist at runtime
consider RadioButtonlist contain Married and UnMarried items
//using database you get marital status value
//string maritialstatus=objdataset.Tables[0].Rows[0]["MaritialStatus"].ToString();
string maritialStatus=”Married”;
rbtnMartialstatus.SelectedValue = maritialStatus;
