aspnet
  1. aspnet-radiobutton

RadioButton - (ASP.NET Web Forms)

RadioButton is a standard control in ASP.NET Web Forms that allows a user to select a single item from a list of options. It is commonly used in forms or surveys where a user is required to make a single choice from a list of options.

Syntax

To create a RadioButton control, you can use the following syntax:

<asp:RadioButton ID="RadioButton1" runat="server" Text="Option 1" />

This creates a single RadioButton control with the ID RadioButton1 and the label Option 1, which will be displayed to the user.

Example

Here's an example of how to use the RadioButton control in ASP.NET Web Forms:

<asp:RadioButton ID="RadioButton1" runat="server" Text="Option 1" GroupName="Group1" Checked="True" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="Option 2" GroupName="Group1" />

In this example, we have created two RadioButton controls. The first control has the text "Option 1" and is pre-selected (Checked="True") by default. The second control has the text "Option 2". Both controls have been grouped together using the GroupName attribute.

Output

When the page is rendered, the RadioButton controls will be displayed to the user. The user can select one of the options by clicking on the RadioButton. Only one item can be selected at a time.

Explanation

The GroupName attribute in the RadioButton control is used to group together a set of RadioButton controls. When a user selects one option, all the other options in the same group are automatically deselected. This ensures that only one option can be selected at a time.

By default, the first RadioButton control in a group is selected. You can use the Checked attribute to change the default selection.

Use

The RadioButton control is commonly used in forms and surveys where a user is required to make a single choice from a list of options. You can group together multiple RadioButton controls if you want to present the user with a larger set of options.

You can use the value of the selected RadioButton control to perform further actions, such as displaying different content on the page, based on the user's selection.

Important Points

  • The GroupName attribute is used to group together a set of RadioButton controls.
  • By default, the first RadioButton control in a group is selected.
  • Only one item can be selected at a time.

Summary

In this page, we discussed the RadioButton control in ASP.NET Web Forms. We covered the syntax, example, output, explanation, use, important points, and summary of the RadioButton control. By using RadioButton controls in your forms, you can provide users with a simple and intuitive way to make a single selection from a list of options.

Published on: