防止在多个下拉列表中选择相同的值

发布于 2024-09-11 23:30:01 字数 65 浏览 3 评论 0原文

我有四个具有相同项目/值的下拉列表。我想防止在上传表单时多次选择相同的值。我如何使用自定义验证器来做到这一点?谢谢!

I have four dropdown lists with same items/values. I want to prevent the same value is chosen more than once when the form is uploaded. How can I do that using a custom validator? Thanks!

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

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

发布评论

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

评论(3

你与昨日 2024-09-18 23:30:01

我主张 Kyra 的第一个解决方案是修改下拉列表,以便无法选择相同的值,预防问题总是比告诉用户他们做错了什么要好。但是,如果您确实想使用 CustomValidator 下面的代码可以工作:

<asp:CustomValidator ID="dropDownValidation" runat="server" OnServerValidate="dropDownValidation_ServerValidate"
    ErrorMessage="The same value cannot be selected in more than one drop down." />

然后在后面的代码或脚本标记中。

protected void dropDownValidation_ServerValidate(object sender, ServerValidateEventArgs e)
{
    e.IsValid = !haveSameValue(DropDownList1.SelectedValue, DropDownList2.SelectedValue) &&
                !haveSameValue(DropDownList1.SelectedValue, DropDownList3.SelectedValue) &&
                !haveSameValue(DropDownList1.SelectedValue, DropDownList4.SelectedValue) &&
                !haveSameValue(DropDownList2.SelectedValue, DropDownList3.SelectedValue) &&
                !haveSameValue(DropDownList2.SelectedValue, DropDownList4.SelectedValue) &&
                !haveSameValue(DropDownList3.SelectedValue, DropDownList4.SelectedValue);
}

protected bool haveSameValue(string first, string second)
{
    if (first != null && second != null)
    {
        return first.Equals(second);
    }

    return first == null && second == null;
}

显然,这可以进一步细化,如果需要,可以使用 JavaScript 函数来提供客户端验证,使用 ClientValidationFunction 属性。

I would advocate Kyra's first solution to modify the dropdown lists so that the same value cannot be selected, it is always better to prevent problems than tell the user what they did was wrong. However, if you do want to use a CustomValidator the following code would work:

<asp:CustomValidator ID="dropDownValidation" runat="server" OnServerValidate="dropDownValidation_ServerValidate"
    ErrorMessage="The same value cannot be selected in more than one drop down." />

And then in the code behind, or a script tag.

protected void dropDownValidation_ServerValidate(object sender, ServerValidateEventArgs e)
{
    e.IsValid = !haveSameValue(DropDownList1.SelectedValue, DropDownList2.SelectedValue) &&
                !haveSameValue(DropDownList1.SelectedValue, DropDownList3.SelectedValue) &&
                !haveSameValue(DropDownList1.SelectedValue, DropDownList4.SelectedValue) &&
                !haveSameValue(DropDownList2.SelectedValue, DropDownList3.SelectedValue) &&
                !haveSameValue(DropDownList2.SelectedValue, DropDownList4.SelectedValue) &&
                !haveSameValue(DropDownList3.SelectedValue, DropDownList4.SelectedValue);
}

protected bool haveSameValue(string first, string second)
{
    if (first != null && second != null)
    {
        return first.Equals(second);
    }

    return first == null && second == null;
}

This can obviously be further refined, and a javascript function can be used to provide client side validation if desired using the ClientValidationFunction property.

陌上青苔 2024-09-18 23:30:01

这可能不是最好的答案,但您始终可以向每个下拉列表添加一个动作侦听器,以便每当其中一个下拉列表的选择发生更改时都会调用它。

这样,当第二个下拉列表中的选定元素发生更改时,它会调用其动作侦听器,然后在其中重置其他下拉列表,以便它们不显示所选值,或者如果可能的话进行设置,以便您无法选择该值

当其中一个下拉列表的选择发生变化时,您检查它是否等于其他下拉列表中的其他选定值,如果是,则向用户显示一个消息框和/或将选择更改为空白选择或第一个尚未使用的可用值。

This might not be the best answer but you can always add an actionlistener to each dropdown list so that it is called whenever the selection changes for on of the dropdown lists.

This way when the selected element in the second dropdown list changes it calls its actionlistener and then within there you reset the other dropdown lists so that they don't show that selected value or if possible set it so you can't select that value

OR

When the selection changes for one of the dropdown lists you check to see if it equals on of the other selected values in the other dropdown lists and if it does you either have a msgbox displayed to the user and/or change the selection to a blank selection or the first available value that isn't already used.

半透明的墙 2024-09-18 23:30:01

只需对 hasSameValue 函数做一些工作即可。这似乎对我有用。感谢您的帮助。

 protected bool haveSameValue(string first, string second)

{
if (!string.IsNullOrEmpty(first) & !string.IsNullOrEmpty(second) && first.Equals(second)) {
    return first.Equals(second);
}

}

Just a bit work on the haveSameValue function. This seems to work for me. Thanks for your help.

 protected bool haveSameValue(string first, string second)

{
if (!string.IsNullOrEmpty(first) & !string.IsNullOrEmpty(second) && first.Equals(second)) {
    return first.Equals(second);
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文