ASP.NET - List Control - Radiobutton
A radiobutton list is a user interface element that presents a list of options, where only one option can be selected at a time. Each option is represented by a radiobutton, which is a small circular button that can be selected by the user. When a radiobutton is selected, it is highlighted and the other radiobuttons in the list are automatically deselected.
Here are some of the main events, methods, and properties associated with radiobutton lists:
Events:
- SelectedIndexChanged: This event is triggered when the selected radiobutton in the list changes.
Methods:
- Add: Adds a new radiobutton to the list.
- Clear: Removes all radiobuttons from the list.
- FindByText: Finds the radiobutton with the specified text in the list.
- FindByValue: Finds the radiobutton with the specified value in the list.
Properties:
- SelectedIndex: Gets or sets the index of the selected radiobutton in the list.
- SelectedValue: Gets or sets the value of the selected radiobutton in the list.
- Items: Gets a collection of all the radiobuttons in the list.
Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioButtonListExample.aspx.cs" Inherits="WebApplication1.RadioButtonListExample" %>
Select Your Country of Residence
In the code-behind file, we can handle the Click event of the submit button and retrieve the selected country from the radio button list:
using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class RadioButtonListExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
string selectedCountry = CountryList.SelectedValue;
ResultLabel.Text = "Your country of residence is: " + selectedCountry;
}
}
}
When the user clicks the submit button, the selected country is retrieved and displayed in a label on the page.