RadioButtonList check if selected
RadioButtonList web server control render a single selection radio button group in web browser.
so users can select only one item from the radiobuttonlist control at a time. the following asp.net
c# example code demonstrate us how can we check (determine) that a radiobuttonlist have any item selected or not.
RadioButtonList SelectedIndex property get or set the lowest ordinal index of the selected items in the list. the default value of SelectedIndex property is -1 which indicates that no item is selected. radiobuttonlist items indexes are start from zero (0).
So we can test the selected index value to determine that any item in radiobuttonlist is selected or not. if the SelectedIndex property value is lower than zero (0) or equal to -1 then the radiobuttonlist is not selected. and if the SelectedIndex property value equal to zero or greater than zero then one item is selected in radiobuttonlist.
RadioButtonList SelectedIndex property get or set the lowest ordinal index of the selected items in the list. the default value of SelectedIndex property is -1 which indicates that no item is selected. radiobuttonlist items indexes are start from zero (0).
So we can test the selected index value to determine that any item in radiobuttonlist is selected or not. if the SelectedIndex property value is lower than zero (0) or equal to -1 then the radiobuttonlist is not selected. and if the SelectedIndex property value equal to zero or greater than zero then one item is selected in radiobuttonlist.
radiobuttonlist-check-if-selected.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
RadioButtonList1.Items.Add(new ListItem("English Oak", "1"));
RadioButtonList1.Items.Add(new ListItem("Pin Oak", "2"));
RadioButtonList1.Items.Add(new ListItem("Scarlet Oak", "3"));
RadioButtonList1.Items.Add(new ListItem("Spanish Oak", "4"));
RadioButtonList1.Items.Add(new ListItem("Swamp White Oak", "5"));
}
if (RadioButtonList1.SelectedIndex < 0)
{
Label1.Text += "you did not checked any item. please check one now...";
}
else
{
Label1.Text = "you selected an item : " + RadioButtonList1.SelectedItem.Text;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net radiobuttonlist check if selected</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - radiobuttonlist check if selected
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
Width="350"
>
</asp:Label>
<br /><br />
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatDirection="Vertical"
AutoPostBack="true"
>
</asp:RadioButtonList>
</div>
</form>
</body>
</html>


- How to change Label width using CSS
- How to databind Label
- How to trim text with ellipsis in a Label
- How to add margin to a Label
- How to get selected value from RadioButtonList
- How to create a horizontal layout RadioButtonList
- How to set a default selected item in RadioButtonList
- How to add margin between items in a RadioButtonList
- How to set RadioButtonList items vertical alignment
- How to add vertical space between items in a RadioButtonList