ASP.NET RadioButton 控制項預設不會自動互斥,必須透過 GroupName 屬性指定同一群組名稱。本文涵蓋靜態宣告、資料繫結控制項 (Repeater/GridView/ListView) 內動態分組、客戶端 jQuery 輔助互斥、伺服器端驗證與無障礙存取 (ARIA) 完整實作。
<asp:RadioButton ID="rdoMale" runat="server" Text="男" GroupName="Gender" />
<asp:RadioButton ID="rdoFemale" runat="server" Text="女" GroupName="Gender" />
<asp:RadioButton ID="rdoOther" runat="server" Text="其他" GroupName="Gender" />
渲染後 HTML name 屬性相同,瀏覽器原生實現互斥。
若在 ItemTemplate 直接寫 GroupName="Group1",所有列會共用同一群組,導致跨列互斥。
<asp:Repeater ID="rptQuestions" runat="server" OnItemDataBound="rptQuestions_ItemDataBound">
<ItemTemplate>
<div class="question">
<%# Eval("QuestionText") %>
<asp:RadioButton ID="rdoYes" runat="server" Text="是" GroupName='<%# "Q_" + Eval("QuestionId") %>' />
<asp:RadioButton ID="rdoNo" runat="server" Text="否" GroupName='<%# "Q_" + Eval("QuestionId") %>' />
</div>
</ItemTemplate>
</asp:Repeater>
protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// 也可在這裡以程式碼動態設定,確保唯一性
var rdoYes = (RadioButton)e.Item.FindControl("rdoYes");
var rdoNo = (RadioButton)e.Item.FindControl("rdoNo");
string uniqueGroup = $"Q_{DataBinder.Eval(e.Item.DataItem, "QuestionId")}";
rdoYes.GroupName = uniqueGroup;
rdoNo.GroupName = uniqueGroup;
}
}
在 RowDataBound (GridView) 或 ItemDataBound (ListView) 中以 DataKey 或資料項 ID 產生唯一 GroupName。
$(function () {
// 針對所有 name 以 "grp_" 開頭的 radio 群組
$(document).on('change', 'input[type=radio][name^="grp_"]', function () {
var name = this.name;
// 取消同群組其他選項的 checked (瀏覽器原生已處理,這裡僅示範觸發 change 事件)
$(`input[type=radio][name="${name}"]`).not(this).each(function () {
this.checked = false;
$(this).trigger('change'); // 觸發自訂 change 事件
});
});
});
// 單一群組
string selectedValue = Request.Form["Gender"]; // name 屬性值
// Repeater 內多群組:遍歷 Items
foreach (RepeaterItem item in rptQuestions.Items)
{
string groupName = $"Q_{((DataRowView)item.DataItem)["QuestionId"]}";
string answer = Request.Form[groupName]; // "on" 或 null
bool isYes = answer == "on"; // 需配合 RadioButton 的 Value 屬性或 ID 判斷
// 建議:給 RadioButton 設定 Value="Y" / Value="N"
}
最佳實踐:為每個 RadioButton 設定 Value 屬性 (如 Value="Y"),伺服端直接讀 Request.Form[groupName] 即得 "Y"/"N"。
<fieldset>
<legend>性別</legend>
<asp:RadioButton ID="rdoMale" runat="server" Text="男" GroupName="Gender"
InputAttributes-aria-describedby="genderDesc" />
<asp:RadioButton ID="rdoFemale" runat="server" Text="女" GroupName="Gender" />
</fieldset>
<span id="genderDesc" class="sr-only">請選擇您的性別</span>
<fieldset> + <legend> 包裹群組。InputAttributes-aria-describedby 關聯說明文字 (需 ASP.NET 4.5+)。| 現象 | 原因 | 解決 |
|---|---|---|
| 跨列互斥 | Repeater 內 GroupName 寫死 | RowDataBound 動態指定唯一 GroupName |
| UpdatePanel 非同步回發後群組失效 | HTML name 屬性被重寫 | 在 endRequest 重新綁定 jQuery 互斥邏輯,或改用原生 GroupName 確保渲染正確 |
| 伺服端取不到值 | 未設定 Value 屬性,Request.Form 回傳 "on" | 明確設定 Value="Y"/"N" 或用 FindControl 取得 Checked 狀態 |
| 巢狀 UserControl 群組衝突 | 多個 UserControl 使用相同 GroupName | GroupName 加上 UserControl ClientID 前綴:GroupName=$"{this.ClientID}_Gender" |