RadioButton Web サーバー コントロール
ページ上に独立したオプション ボタンを作成します。複数のオプション ボタンをグループ化して、一度に 1 つのボタンしか選択できないようにすることができます。
<asp:RadioButtonid="RadioButton1" AutoPostBack="True|False" Checked="True|False" GroupName="GroupName" Text="label" TextAlign="Right|Left" OnCheckedChanged="OnCheckedChangedMethod" runat="server"/>
解説
RadioButton サーバー コントロールは、Web フォーム ページ上にオプション ボタンを作成します。コントロールに表示するテキストを指定するには、Text プロパティを設定します。このテキストは、オプション ボタンの左右のいずれかに表示できます。テキストを表示する側を制御するには、TextAlign プロパティを設定します。複数のオプション ボタンをグループ化するには、各 RadioButton コントロールに同じ GroupName を指定します。オプション ボタンをグループ化して、そのグループ内で一度に 1 つのボタンしか選択できないようにすることができます。
メモ RadioButtonList コントロールを使用することもできます。データ連結を使用してオプション ボタンのセットを作成する場合は、RadioButtonList コントロールを使用する方が簡単です。しかし、個別の RadioButton コントロールを使用する方が、レイアウトを柔軟に制御できます。
RadioButton コントロールが選択されているかどうかを判断するには、Checked プロパティを調べます。
注意 テキストは、RadioButton コントロールに表示される前には HTML エンコードされません。これにより、テキストの HTML タグ内にスクリプトを埋め込むことができるようになります。コントロールの値がユーザーによって入力された場合は、セキュリティの脆弱性への対策として、入力された値を必ず検証してください。
RadioButton Web サーバー コントロールのプロパティとイベントの詳細については、RadioButton クラスのドキュメントを参照してください。
例
RadioButton コントロールを使用して、一度に 1 つしか選択できない複数のオプション ボタンのグループを作成する方法を次の例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script 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 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><p>
<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><p>
<asp:RadioButton id=Radio3
Text="Full"
GroupName="RadioGroup1"
runat="server" /><br>
This option installs all features for the product.
<i>Requires 4.3 MB disk space.</i><p>
<asp:Button text="Submit"
OnClick="SubmitBtn_Click"
runat=server/>
<asp:Label id=Label1
Font-Bold="true"
runat="server" />
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script 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>
<form runat="server">
<h3>RadioButton Example</h3>
<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><p>
<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><p>
<asp:RadioButton id="Radio3"
Text="Full"
GroupName="RadioGroup1"
runat="server"/><br>
This option installs all features for the product.
<i>Requires 4.3 MB disk space.</i><p>
<asp:Button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat=server/>
<asp:Label id="Label1"
Font-Bold="true"
runat="server" />
</form>
</body>
</html>