Share via


RadioButtonList Web 伺服器控制項宣告式語法

更新:2007 年 11 月

建立選項按鈕群組。這個控制項支援繫結至資料來源。

 <asp:RadioButtonList     AccessKey="string"     AppendDataBoundItems="True|False"     AutoPostBack="True|False"     BackColor="color name|#dddddd"     BorderColor="color name|#dddddd"     BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|         Inset|Outset"     BorderWidth="size"     CausesValidation="True|False"     CellPadding="integer"     CellSpacing="integer"     CssClass="string"     DataMember="string"     DataSource="string"     DataSourceID="string"     DataTextField="string"     DataTextFormatString="string"     DataValueField="string"     Enabled="True|False"     EnableTheming="True|False"     EnableViewState="True|False"     Font-Bold="True|False"     Font-Italic="True|False"     Font-Names="string"     Font-Overline="True|False"     Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|         Large|X-Large|XX-Large"     Font-Strikeout="True|False"     Font-Underline="True|False"     ForeColor="color name|#dddddd"     Height="size"     ID="string"     OnDataBinding="DataBinding event handler"     OnDataBound="DataBound event handler"     OnDisposed="Disposed event handler"     OnInit="Init event handler"     OnLoad="Load event handler"     OnPreRender="PreRender event handler"     OnSelectedIndexChanged="SelectedIndexChanged event handler"     OnTextChanged="TextChanged event handler"     OnUnload="Unload event handler"     RepeatColumns="integer"     RepeatDirection="Horizontal|Vertical"     RepeatLayout="Table|Flow"     runat="server"     SelectedIndex="integer"     SelectedValue="string"     SkinID="string"     Style="string"     TabIndex="integer"     TextAlign="Left|Right"     ToolTip="string"     ValidationGroup="string"     Visible="True|False"     Width="size" >             <asp:ListItem                 Enabled="True|False"                 Selected="True|False"                 Text="string"                 Value="string"             /> </asp:RadioButtonList>

備註

RadioButtonList 控制項可以讓您藉由繫結至資料來源,建立以動態方式產生的單一選取式選項按鈕群組。若要指定您希望出現在 RadioButtonList 控制項中的項目 (Item),請將每個項目 (Entry) 的 ListItem 項目 (Element) 置於 RadioButtonList 控制項的開頭和結尾標記之間。

注意事項:

您也可以使用 RadioButton 控制項。RadioButtonList 控制項比較適合於使用資料繫結建立選項按鈕組,而個別的 RadioButton 控制項則可讓您對配置有較好的控制。

RadioButtonList 控制項也支援資料繫結。若要將控制項繫結至資料來源,請先建立資料來源,像是 ArrayList 物件,並在其中包含要在控制項中顯示的項目。接下來,使用 DataBind 方法,將資料來源繫結至 RadioButtonList 控制項。您可以使用 DataTextField 和 DataValueField 屬性來指定資料來源中的欄位,以分別繫結至控制項中每個清單項目的 Text 和 Value 屬性。RadioButtonList 控制項現在將會顯示來自資料來源的資訊。

若要判斷 RadioButtonList 控制項中的選取項目,請逐一查看 Items 集合,並測試集合中每個項目的 Selected 屬性。

您可以用 RepeatLayoutRepeatDirection 屬性來指定清單的呈現方式。如果 RepeatLayout 設定為 Table (預設設定值),清單就會在表格內呈現。如果設定為 Flow,清單則會以不具任何表格結構的方式呈現。根據預設,RepeatDirection 設定為 Vertical。將這個屬性設定成 Horizontal,便會以水平方式呈現清單。

警告:

尚未顯示於 RadioButtonList 控制項中的文字,便還未經過 HTML 編碼。這樣便可以在文字中的 HTML 標記內嵌入指令碼。如果控制項的值來自使用者輸入,請務必驗證值來協助防止安全性的弱點。

如需 RadioButtonList Web 伺服器控制項之屬性和事件的詳細資訊,請參閱 RadioButtonList 類別文件。

範例

下列範例是示範如何使用 RadioButtonList 控制項對使用者顯示一組互斥 (Mutually Exclusive) 的選項。

<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
    <title>RadioButtonList Example</title>
<script language="VB" runat="server">

    Sub Button1_Click(Source As Object, e As EventArgs)
        If RadioButtonList1.SelectedIndex > - 1 Then
            Label1.Text = "You selected: " & RadioButtonList1.SelectedItem.Text
        End If
    End Sub

    Sub chkLayout_CheckedChanged(sender As Object, e As EventArgs)        
        If chkLayout.Checked = True Then
            RadioButtonList1.RepeatLayout = RepeatLayout.Table
        Else
            RadioButtonList1.RepeatLayout = RepeatLayout.Flow
        End If
    End Sub

    Sub chkDirection_CheckedChanged(sender As Object, e As EventArgs)        
        If chkDirection.Checked = True Then
            RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal
        Else
            RadioButtonList1.RepeatDirection = RepeatDirection.Vertical
        End If
    End Sub

     </script>

 </head>
 <body>

     <h3>RadioButtonList Example</h3>

     <form id="form1" runat="server">

         <asp:RadioButtonList id="RadioButtonList1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>

         <br />

         <asp:CheckBox id="chkLayout" OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked="true" AutoPostBack="true" runat="server" />

         <br />

         <asp:CheckBox id="chkDirection" OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />

         <br />

         <asp:Button id="Button1" Text="Submit" onclick="Button1_Click" runat="server"/>

         <br />

         <asp:Label id="Label1" font-names="Verdana" font-size="8pt" runat="server"/>

     </form>

 </body>
 </html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
    <title>RadioButtonList Example</title>
<script language="C#" runat="server">

        void Button1_Click(object Source, EventArgs e) 
        {
           if (RadioButtonList1.SelectedIndex > -1) 
           {  
              Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
           }
        }

        void chkLayout_CheckedChanged(Object sender, EventArgs e) 
        {

           if (chkLayout.Checked == true) 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Table;
           }
           else 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
           }     
        }

        void chkDirection_CheckedChanged(Object sender, EventArgs e) 
        {

           if (chkDirection.Checked == true) 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
           }
           else 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
           }  
        }

     </script>

 </head>
 <body>

     <h3>RadioButtonList Example</h3>

     <form id="form1" runat="server">

         <asp:RadioButtonList id="RadioButtonList1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>

         <br />

         <asp:CheckBox id="chkLayout" OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked="true" AutoPostBack="true" runat="server" />

         <br />

         <asp:CheckBox id="chkDirection" OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />

         <br />

         <asp:Button id="Button1" Text="Submit" onclick="Button1_Click" runat="server"/>

         <br />

         <asp:Label id="Label1" font-names="Verdana" font-size="8pt" runat="server"/>

     </form>

 </body>
 </html>

<%@ Page Language="JScript" AutoEventWireup="True" %>
<!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>
    <title>RadioButtonList Example</title>
<script language="JScript" runat="server">

        function Button1_Click(Source : System.Object, e : EventArgs) 
        {
           if (RadioButtonList1.SelectedIndex > -1) 
           {  
              Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
           }
        }

        function chkLayout_CheckedChanged(sender : System.Object, e : EventArgs) 
        {

           if (chkLayout.Checked == true) 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Table;
           }
           else 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
           }     
        }

        function chkDirection_CheckedChanged(sender : System.Object, e : EventArgs) 
        {

           if (chkDirection.Checked == true) 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
           }
           else 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
           }  
        }

     </script>

 </head>
 <body>

     <h3>RadioButtonList Example</h3>

     <form id="form1" runat="server">

         <asp:RadioButtonList id="RadioButtonList1" runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>

         <br />

         <asp:CheckBox id="chkLayout" OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked="true" AutoPostBack="true" runat="server" />

         <br />

         <asp:CheckBox id="chkDirection" OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />

         <br />

         <asp:Button id="Button1" Text="Submit" onclick="Button1_Click" runat="server"/>

         <br />

         <asp:Label id="Label1" font-name="Verdana" font-size="8pt" runat="server"/>

     </form>

 </body>
 </html>

請參閱

參考

RadioButtonList

其他資源

Web 伺服器控制項語法