閱讀此文請先查看網頁教學網的:ASP.NET入門教程:Web服務器控件,簡單講述了Web服務器控件的使用方法。
RadioButton 控件用於顯示單選按鈕。在頁上創建一個單選按鈕。可將多個單選按鈕分為一組以提供一組互相排斥的選項。
提示:如需創建一系列使用數據綁定的單選按鈕,請使用 RadioButtonList 控件!
AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
<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,則可以將多個單選按鈕分為一組。將單選按鈕分為一組將只允許從該組中進行互相排斥的選擇。
注意:您還可以使用 RadioButtonList 控件。對於使用數據綁定創建一組單選按鈕而言,RadioButtonList 控件更易於使用,而單個 RadioButton 控件則使您能夠更好地控制布局。
若要確定 RadioButton 控件是否已選中,請測試 Checked 屬性。
警告:文本在 RadioButton 控件中顯示之前並非 HTML 編碼形式。這使得可以在文本中的 HTML 標記中嵌入腳本。如果控件的值是由用戶輸入的,請務必要對輸入值進行驗證以防止出現安全漏洞。
下面的示例演示如何使用 RadioButton 控件為用戶提供一組互斥的選項。
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<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 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 runat="server" Text="Full" GroupName="RadioGroup1" /><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 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 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 runat="server" Text="Full" GroupName="RadioGroup1" /><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>