views:

40

answers:

3

Hi, 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!

A: 

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.

Kyra
Thanks very much. I don't know what an actionlistener is but I will research your solution and implement it if I find it better than a validator.
netNewbi3
+1  A: 

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.

sgriffinusa
Hi, many thanks for your help. The custom validator works fine. I will also take your advice and code a client validation function too. Could you let me know how your server validation would accept null values as well? Thanks!
netNewbi3
I updated my answer to handle null values. Please note that I haven't actually tried this, so it should be used as a reference.
sgriffinusa
A: 

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);
}

}

netNewbi3