How to use RadioButtonList radio button group control in ASP. NET

  • 2021-07-18 07:49:00
  • OfStack

The RadioButtonList control represents a list control that encapsulates a set of radio button controls.

You can use two types of ASP. NET controls to add radio buttons to a Web page: individual RadioButton controls or one RadioButtonList control. Both types of controls allow users to choose from 1 group of mutually exclusive predefined options. Using these controls, you can define any number of labeled radio buttons and arrange them horizontally or vertically.

1. Common properties

属性 作用
RepeatDirection Horizontal|Vertical 项的布局方向:水平方向|竖直风向
RepeatLayout Table|Flow 展现方式:表格|流线型
Selected True|Falsee 是否为选中状态

2. Code demo

Front Desk RadioButtonList. aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadioButtonList.aspx.cs" Inherits="WebControls_RadioButtonList" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>RadioButtonList (Radio Button Group) </h3>
        <hr />
        Please select gender: <asp:RadioButtonList
            ID="radlSex" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Selected="True"> Male </asp:ListItem>
            <asp:ListItem> Female </asp:ListItem>
        </asp:RadioButtonList>
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text=" Submit " onclick="btnSubmit_Click" />
        <hr />
        The gender you chose is: <asp:Label ID="lblState" runat="server"></asp:Label>
      
    </div>
    </form>
</body>
</html>

Background RadioButtonList. aspx. cs


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class WebControls_RadioButtonList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblState.Text = radlSex.SelectedValue;
    }
}


Related articles: