ASP.NET中 RadioButtonList 單選按鈕組控件的使用方法

字號:


    本文主要簡單介紹RadioButtonList控件的常見屬性和使用方法,希望對大家有所幫助。
    RadioButtonList 控件表示一個封裝了一組單選按鈕控件的列表控件。
    可以使用兩種類型的 ASP.NET 控件將單選按鈕添加到網(wǎng)頁上:各個 RadioButton 控件或一個 RadioButtonList 控件。這兩類控件都允許用戶從一小組互相排斥的預(yù)定義選項中進(jìn)行選擇。使用這些控件,可定義任意數(shù)目的帶標(biāo)簽的單選按鈕,并將它們水平或垂直排列。
    一、常用屬性
    
屬性 作用
Repeatdirection Horizontal|Vertical 項的布局方向:水平方向|豎直風(fēng)向
RepeatLayout table|Flow 展現(xiàn)方式:表格|流線型
Selected true|Falsee 是否為選中狀態(tài)

    二、代碼演示
    前臺 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(單選按鈕組)</h3>
            <hr />
            請選擇性別:<asp:RadioButtonList 
                ID="radlSex" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
                <asp:ListItem Selected="True">男</asp:ListItem>
                <asp:ListItem>女</asp:ListItem>
            </asp:RadioButtonList>
            <br />
            <asp:Button ID="btnSubmit" runat="server" Text="提交" onclick="btnSubmit_Click" />
            <hr />
            你選擇的性別為:<asp:Label ID="lblState" runat="server"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    后臺 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;
        }
    }