在網頁上建立個別選項按鈕。您可以將多個選項按鈕群組在一起以提供互斥的選項組。
<asp:RadioButtonid="RadioButton1" AutoPostBack="True|False" Checked="True|False" GroupName="GroupName" Text="label" TextAlign="Right|Left" OnCheckedChanged="OnCheckedChangedMethod" runat="server"/>
備註
RadioButton 伺服器控制項會在 Web Form 網頁上建立選項按鈕。請設定 Text 屬性來指定要在控制項中顯示的文字。這個文字可以出現在選項按鈕的左邊或是右邊。請設定 TextAlign 屬性來控制文字要出現在哪一邊。如果對每一個 RadioButton 控制項指定同樣的 GroupName,您就可以將多個選項按鈕群組在一起。將選項按鈕群組在一起,將會只允許從群組中選取一個互斥的選項。
**注意 **您也可以使用 RadioButtonList 控制項。RadioButtonList 控制項比較適合於使用資料繫結來建立選項按鈕組,而個別的 RadioButton 控制項則可讓您對配置有較好的控制。
若要判斷 RadioButton 控制項是否已被選取,請測試 Checked 屬性。
**警告 **文字顯示於 RadioButton 控制項前,並不是 HTML 編碼。這樣便可以在文字中的 HTML 標記內嵌入指令碼。如果控制項的值來自使用者輸入,請務必驗證值來協助防止安全性的弱點。
如需 RadioButton Web 伺服器控制項之屬性和事件的詳細資訊,請參閱 RadioButton 類別文件。
範例
下列範例是示範如何使用 RadioButton 控制項對使用者提供一組互斥的選項。
<%@ 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>