如何确认至少选择了1个单选按钮?
答案 0 :(得分:6)
如果所有单选按钮都是同一 RadioButtonList 的一部分,则可以使用简单的 RequiredFieldValidator 。这是一个例子:
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatColumns="3">
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Yellow</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="You must Select your favorite color!">*
</asp:RequiredFieldValidator>
如果RadioButton位于不同的RadioButtonLists中,并且您想验证在多个列表中至少选择了一个,那么您需要 CustomValidator。
答案 1 :(得分:2)
另一种方法可能是,如果您在页面加载期间默认选中其中一个(可能是您认为大多数用户将选择的那个),那么用户将被“强制”更改选择或继续使用默认选项,确保始终选择一个选项的方式。