次の方法で共有


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 コントロールに表示する項目を指定するには、RadioButtonList コントロールの開始タグと終了タグの間に、各エントリの ListItem 要素を挿入します。

y7k30eyz.alert_note(ja-jp,VS.90).gifメモ :

また、RadioButton コントロールも使用できます。データ バインディングを使用してオプション ボタンのセットを作成する場合は、RadioButtonList コントロールを使用する方が簡単です。しかし、個別の RadioButton コントロールを使用する方が、レイアウトを柔軟に制御できます。

RadioButtonList コントロールは、データ バインディングもサポートします。コントロールをデータ ソースにバインドするには、まず ArrayList オブジェクトなど、コントロールに表示するアイテムが含まれるデータ ソースを作成します。次に、DataBind メソッドを使用して、データ ソースを RadioButtonList コントロールにバインドします。DataTextField プロパティと DataValueField プロパティを使用して、コントロール内の各リスト項目の Text プロパティと Value プロパティにバインドするデータ ソース フィールドをそれぞれ指定します。これで、RadioButtonList コントロールに、データ ソースの情報が表示されるようになります。

RadioButtonList コントロールで選択されている項目を確認するには、Items コレクションを反復処理して、コレクション内の各項目の Selected プロパティを調べます。

RepeatLayout プロパティと RepeatDirection プロパティを使用して、リストの表示方法を指定できます。RepeatLayoutTable に設定されている場合 (既定の設定)、リストはテーブル内に表示されます。Flow に設定されている場合、リストはテーブル構造を使用せずに表示されます。既定では、RepeatDirectionVertical に設定されています。このプロパティを Horizontal に設定すると、リストは水平方向に表示されます。

y7k30eyz.alert_caution(ja-jp,VS.90).gif注意 :

テキストは、RadioButtonList コントロールに表示される前には HTML エンコードされません。これにより、テキストの HTML タグ内にスクリプトを埋め込むことができるようになります。コントロールの値がユーザーによって入力された場合は、セキュリティの脆弱性への対策として、入力された値を必ず検証してください。

RadioButtonList Web サーバー コントロールのプロパティとイベントの詳細については、RadioButtonList クラスのドキュメントを参照してください。

使用例

RadioButtonList コントロールを使用して、一度に 1 つしか選択できない複数のオプション ボタンのグループを表示する方法を次の例に示します。

<%@ 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 サーバー コントロール構文