【问题标题】:How can I validate that at least 1 radio button is selected?如何验证至少选择了 1 个单选按钮?
【发布时间】:2010-10-23 08:03:40
【问题描述】:

如何验证是否至少选择了 1 个单选按钮?

【问题讨论】:

    标签: asp.net radio-button


    【解决方案1】:

    如果所有单选按钮都属于同一个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>
    

    如果您在不同的 RadioButtonLists 中有 RadioButtons,并且您想验证在多个列表中至少选择了一个,那么您需要一个 CustomValidator

    【讨论】:

      【解决方案2】:

      另一种方法可能是,如果您默认选中其中一个,则在页面加载期间(可能是您认为大多数用户会选择的那个),那么用户将被“强制”更改选择或继续使用默认选项,在此确保始终有一个选项被选中的方式。

      【讨论】:

      • 在同样的情况下,您希望强制用户做出选择。创建默认选项不是一回事。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 2015-11-06
      相关资源
      最近更新 更多