ASP.NET - Radio Button

A radio button is a graphical user interface element that allows users to select one option from a list of mutually exclusive options. Here's an explanation of the properties, events, and methods of radio buttons:

Properties:

  • Checked: Determines whether the radio button is selected or not. This is a Boolean property, and it can be set or retrieved using code.
  • Text: The text displayed next to the radio button. This is a string property that can be set or retrieved using code.
  • ID: The unique identifier for the radio button. This is a string property that can be set in the design view or in code.
  • GroupName: The name of the radio button group to which this radio button belongs. All radio buttons with the same group name are mutually exclusive, meaning that selecting one deselects all the others. This is a string property that can be set in the design view or in code.

Events:

  • CheckedChanged: Occurs when the Checked property of the radio button changes. This event can be handled in code to perform an action when the user selects or deselects the radio button.
  • Click: Occurs when the user clicks on the radio button. This event can be handled in code to perform an action when the user interacts with the radio button.

Methods:

  • ClearSelection: Deselects all radio buttons in the same group as this radio button.
  • Focus: Sets the focus to the radio button, making it the active element on the page.
  • ToString: Returns a string representation of the radio button, which can be useful for debugging or logging purposes.

Overall, radio buttons are a simple and effective way to present users with a set of mutually exclusive options, and the properties, events, and methods of radio buttons provide developers with the flexibility to create dynamic and responsive user interfaces.

Example

  • First, create a new ASP.NET Web Forms project in Visual Studio.
  • In the default Web Form that is created, add a RadioButtonList control to the page by dragging and dropping it from the Toolbox.
  • Give the RadioButtonList a meaningful ID in the Properties window, such as "rblGrade".
  • Add some text next to the radio button list to indicate what it is for, such as "Please select your grade".
  • Add two or more radio buttons to the radio button list. Each radio button represents a different grade, and they should all have the same GroupName property value to ensure that they are mutually exclusive.
  • In the code-behind file (usually named Default.aspx.cs), you can access the selected value of the radio button list using the ID you gave it. For example, you could write a method that gets called when a button is clicked, and in that method you could check the selected value of the radio button list like this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
    string selectedGrade = rblGrade.SelectedValue;
    switch (selectedGrade)
    {
        case "Grade10":
            // Do something if Grade 10 is selected
            break;
        case "Grade11":
            // Do something if Grade 11 is selected
            break;
        case "Grade12":
            // Do something if Grade 12 is selected
            break;
        default:
            // Do something if no grade is selected
            break;
    }
}

This code retrieves the selected value of the radio button list with ID "rblGrade", and then uses a switch statement to perform different actions depending on which grade was selected.