如何检查是否至少有一个 RadioButtonList 选择了一项?

发布于 2024-10-19 15:03:03 字数 128 浏览 6 评论 0原文

我在一个页面上有 20 个 RadioButtonList

我需要创建一种验证方法,以确保这些 RadioButtonList 中至少有一个已选定项目。

我需要为此使用什么样的验证?

I have 20 RadioButtonLists on a page.

I need to create a validation method to ensure that at least one of these RadioButtonLists has an item selected.

What kind of validation would I need to use for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不气馁 2024-10-26 15:03:03

编辑:根据评论和澄清更新了问题。

如果您要针对多个 RadioButtonList 进行验证,那么您需要使用 CustomValidator 并实现服务器端检查。

以下是一些测试标记:

<asp:RadioButtonList ID="rblstTest1" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList>
<br /><br />
<asp:RadioButtonList ID="rblstTest2" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList><br />
<asp:Button ID="btnTestRb" runat="server" ValidationGroup="Test" Text="Test RBL" 
    OnClick="btnTestRb_Click" />
<asp:CustomValidator runat="server" ValidationGroup="Test" ID="cvTest" 
    ControlToValidate="rblstTest1" OnServerValidate="cvTest_ServerValidate" 
    ValidateEmptyText="true" Enabled="true" display="Dynamic" SetFocusOnError="true"
    ErrorMessage="You must select at least one item." /> 

使用以下扩展方法查找所有 RadioButtonList 控件() :

static class ControlExtension
{
    public static IEnumerable<Control> GetAllControls(this Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control descendant in control.GetAllControls())
            {
                yield return descendant;
            }
        }
    }
} 

然后实现服务器端 CustomValidator 检查:

protected void cvTest_ServerValidate(object sender, ServerValidateEventArgs e)
{            
    int count = this.GetAllControls().OfType<RadioButtonList>().
        Where(lst => lst.SelectedItem != null).Count();
    e.IsValid = (count > 0);
 }

我已经测试了上面的示例,它似乎完全符合您的需要。您应该能够很容易地将其切换到 VB。希望这能解决您的问题。

EDIT: Updated question based on comments and clarification.

If you are validating against multiple RadioButtonLists then you need to use a CustomValidator and implement the server side check.

Here is some test markup:

<asp:RadioButtonList ID="rblstTest1" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList>
<br /><br />
<asp:RadioButtonList ID="rblstTest2" runat="server" ValidationGroup="Test">
    <asp:ListItem Text="Test 1" Value="1" />
    <asp:ListItem Text="Test 2" Value="2" />
    <asp:ListItem Text="Test 3" Value="3" />
</asp:RadioButtonList><br />
<asp:Button ID="btnTestRb" runat="server" ValidationGroup="Test" Text="Test RBL" 
    OnClick="btnTestRb_Click" />
<asp:CustomValidator runat="server" ValidationGroup="Test" ID="cvTest" 
    ControlToValidate="rblstTest1" OnServerValidate="cvTest_ServerValidate" 
    ValidateEmptyText="true" Enabled="true" display="Dynamic" SetFocusOnError="true"
    ErrorMessage="You must select at least one item." /> 

Use the following extension method to find all the RadioButtonList controls (Source):

static class ControlExtension
{
    public static IEnumerable<Control> GetAllControls(this Control parent)
    {
        foreach (Control control in parent.Controls)
        {
            yield return control;
            foreach (Control descendant in control.GetAllControls())
            {
                yield return descendant;
            }
        }
    }
} 

Then implement the server side CustomValidator check:

protected void cvTest_ServerValidate(object sender, ServerValidateEventArgs e)
{            
    int count = this.GetAllControls().OfType<RadioButtonList>().
        Where(lst => lst.SelectedItem != null).Count();
    e.IsValid = (count > 0);
 }

I have tested the above example and it seems to do exactly what you need. You should be able to switch it to VB pretty easily. Hope this solves your problem.

娇纵 2024-10-26 15:03:03

您可以为 RadioButtonList 设置默认值,这意味着用户永远无法不选择选项,并且您不需要所有验证代码

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Selected="True">Never</asp:ListItem>
    <asp:ListItem>Twice A Week</asp:ListItem>
    <asp:ListItem>Every Day Baby!</asp:ListItem>
</asp:RadioButtonList>

编辑
正如下面的评论所指出的,这本身不足以作为验证手段。在服务器端验证所有用户输入也是一种很好的做法。

You could set a default for your RadioButtonList which would mean a user could never not select an option and you would not need all the validation code

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Selected="True">Never</asp:ListItem>
    <asp:ListItem>Twice A Week</asp:ListItem>
    <asp:ListItem>Every Day Baby!</asp:ListItem>
</asp:RadioButtonList>

EDIT
As pointed out in a comment below this would not be sufficient by itself as a means of validation. It is good practice to validate all user input on the server-side also.

清风疏影 2024-10-26 15:03:03

我使用适用于 ListControls 的扩展方法

        public static bool IsAnyItemSelected(this ListControl input) { return input.Items.Cast<ListItem>().Aggregate(false, (current, listItem) => current | listItem.Selected); }

I use an extension method that works for ListControls

        public static bool IsAnyItemSelected(this ListControl input) { return input.Items.Cast<ListItem>().Aggregate(false, (current, listItem) => current | listItem.Selected); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文