次の方法で共有


RadioButton Web サーバー コントロールの宣言構文

更新 : 2007 年 11 月

ページ上に独立したオプション ボタンを作成します。複数のオプション ボタンをグループ化して、一度に 1 つのボタンしか選択できないようにすることができます。

<asp:RadioButton
    AccessKey="string"
    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"
    Checked="True|False"
    CssClass="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"
    GroupName="string"
    Height="size"
    ID="string"
    OnCheckedChanged="CheckedChanged event handler"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    TextAlign="Left|Right"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

解説

RadioButton サーバー コントロールは、Web フォーム ページ上にオプション ボタンを作成します。コントロールに表示するテキストを指定するには、Text プロパティを設定します。このテキストは、オプション ボタンの左右のいずれかに表示できます。テキストを表示する側を制御するには、TextAlign プロパティを設定します。複数のオプション ボタンをグループ化するには、各 RadioButton コントロールに同じ GroupName を指定します。オプション ボタンをグループ化して、そのグループ内で一度に 1 つのボタンしか選択できないようにすることができます。

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

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

RadioButton コントロールが選択されているかどうかを判断するには、Checked プロパティを調べます。

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

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

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

使用例

RadioButton コントロールを使用して、一度に 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>RadioButton Example</title>
<script language="VB" runat="server">

    Sub SubmitBtn_Click(Sender As Object, e As EventArgs)

        If Radio1.Checked Then
            Label1.Text = "You selected " & Radio1.Text
        ElseIf Radio2.Checked Then
            Label1.Text = "You selected " & Radio2.Text
        ElseIf Radio3.Checked Then
            Label1.Text = "You selected " & Radio3.Text
        End If
    End Sub

     </script>

 </head>
 <body>

     <h3>RadioButton Example</h3>

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

         <h4>Select the type of installation you want to perform:</h4>

         <asp:RadioButton id="Radio1" Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /><br />

         This option installs the features most typically used.  <i>Requires 1.2 MB disk space.</i><br />

         <asp:RadioButton id="Radio2" Text="Compact" GroupName="RadioGroup1" runat="server"/><br />

         This option installs the minimum files required to run the product.  <i>Requires 350 KB disk space.</i><br />

         <asp:RadioButton id="Radio3" runat="server" Text="Full" GroupName="RadioGroup1" /><br />

         This option installs all features for the product.  <i>Requires 4.3 MB disk space.</i><br />

         <asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server"/>

         <asp:Label id="Label1" font-bold="true" 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>RadioButton Example</title>
<script language="C#" runat="server">

         void SubmitBtn_Click(Object Sender, EventArgs e) {

             if (Radio1.Checked) {
                 Label1.Text = "You selected " + Radio1.Text;
             }
             else if (Radio2.Checked) {
                 Label1.Text = "You selected " + Radio2.Text;
             }
             else if (Radio3.Checked) {
                 Label1.Text = "You selected " + Radio3.Text;
             }
         }

     </script>

 </head>
 <body>

     <h3>RadioButton Example</h3>

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

         <h4>Select the type of installation you want to perform:</h4>

         <asp:RadioButton id="Radio1" Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /><br />

         This option installs the features most typically used.  <i>Requires 1.2 MB disk space.</i><br />

         <asp:RadioButton id="Radio2" Text="Compact" GroupName="RadioGroup1" runat="server"/><br />

         This option installs the minimum files required to run the product.  <i>Requires 350 KB disk space.</i><br />

         <asp:RadioButton id="Radio3" runat="server" Text="Full" GroupName="RadioGroup1" /><br />

         This option installs all features for the product.  <i>Requires 4.3 MB disk space.</i><br />

         <asp:button text="Submit" OnClick="SubmitBtn_Click" runat="server"/>

         <asp:Label id="Label1" font-bold="true" runat="server" />

     </form>

 </body>
 </html>

参照

参照

RadioButton

その他の技術情報

Web サーバー コントロール構文