| 屬性 | 型別 | 說明 |
|---|---|---|
| Checked | bool | 是否被選中,單選組中同一時間僅一個為 true |
| AutoCheck | bool | true 時自動取消同組其他項(預設 true) |
| Appearance | Appearance | Normal(預設圓鈕)或 Button(按鈕外觀) |
| Text | string | 顯示文字,可含 & 定義存取鍵(Alt+字母) |
| GroupName | string | WebForms 必須設定相同 GroupName 才視為同一單選組 |
Checked 屬性變更時觸發,需設定 AutoPostBack="true" 才會回傳。<asp:RadioButton ID="rdoMale" GroupName="Gender" Text="男" runat="server" />
<asp:RadioButton ID="rdoFemale" GroupName="Gender" Text="女" runat="server" />
同一 GroupName 的 RadioButton 自動互斥,不需置於 Panel/GroupBox。
foreach (Control c in this.form1.Controls) // 或特定 Panel
{
if (c is RadioButton rb && rb.Checked)
{
string value = rb.Text; // 或 rb.Attributes["value"]
break;
}
}
<asp:RadioButton ... OnCheckedChanged="Gender_Changed" AutoPostBack="true" />
protected void Gender_Changed(object sender, EventArgs e)
{
var rb = (RadioButton)sender;
if (rb.Checked)
{
// 處理選中邏輯
}
}
IsChecked 三態(true/false/null)。<label> 或 AssociatedControlID 提升點擊區域。