1
votes

When having a ASP.NET validation control, the triggering of the showing and hiding the error-message only concern that specific validation control. Meaning, if that control fails, it is only that controls error-message that is shown.

But I have two dropdownlists that should follow each others error-messages. They are only valid if both have a value or no value at all.

This is my code:

<script type="text/javascript" language="javascript">    
    function ValidateDDL(source, args) {
            var ddl_1 = $get('<%= DropDownList1.ClientID %>');
            var ddl_2 = $get('<%= DropDownList2.ClientID %>');
            var value_1 = ddl_1.options[ddlMailValue.selectedIndex].value;
            var value_2 = ddl_1.options[ddlMailValue.selectedIndex].value;

            if ((value_1== "" && value_2 != "") || (value_1 != "" && value_2 == "")) {
                    args.IsValid = false;
            }
            else {
                    args.IsValid = true;
            }
    }
</script>

<asp:DropDownList ID="DropDownList1" runat="server" CausesValidation="true"></asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="DropDownList1" ValidateEmptyText="true" ClientValidationFunction="ValidateDDL" ></asp:CustomValidator>

<asp:DropDownList ID="DropDownList2" runat="server" CausesValidation="true"></asp:DropDownList>
<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="DropDownList2" ValidateEmptyText="true" ClientValidationFunction="ValidateDDL" ></asp:CustomValidator>

But this mean, that when I change the first dropdownlist (from having a value to not have), it validate 'false' because the second dropdownlist don't have a value yet. But when I change the second dropdownlist to also contain a value, the first error-message is still visible because that control is not validated. Off course, this has only visual concern, because the controls will validate 'true' when I try to save.

But still... Is there a way to get hold of the first validation control and set id to be the same state as the second (and vice versa)?

1

1 Answers

0
votes

Have you thought about just having the validator trigger on the save only? Instead of having them trigger as soon as you enter values you can just hold off on that until the save actually happens. Just assign the ValidationGroup to both your validators and your save button and only have it validate on save.