Link to home
Start Free TrialLog in
Avatar of GLIanimal
GLIanimal

asked on

Custom Validator firing, but does not stop the button click

I currently have added a custom validator to my asp.net page. This CV checks to see if the value of a certain ddl is one of 2 values. My code seems to fire, but it continues with the button click code rather than stopping the user and requiring them to select the required value. Am I missing something here?
Thanks

CV.Properties; enabled = true, ValidateEmptyText = true.
 
 
protected void SubCatCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if(ddProdCat.SelectedValue != "NEW PRODUCTS" || ddProdCat.SelectedValue != "CUSTOM FABRICATIONS")
        {
            args.IsValid = false;
            SubCatCustomValidator.Text = "**Please Select A Sub Category**";
        }
    }
    protected void SeriesCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (ddProdCat.SelectedValue != "NEW PRODUCTS" || ddProdCat.SelectedValue != "CUSTOM FABRICATIONS")
        {
            args.IsValid = false;
            SubCatCustomValidator.Text = "**Please Select A Series**";
        }
    }

Open in new window

Avatar of tiagosalgado
tiagosalgado
Flag of Portugal image

You've got an error on your logic condition. You need to put a AND and not a OR condition.
By the way, why don't you add a RequiredFieldValidator instead of CustomValidator?
Check this example

<asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Text=" - Select - " Value="0" />
            <asp:ListItem Text=" Option 1 " Value="1" />
            <asp:ListItem Text=" Option 2 " Value="2" />
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="DropDownList1" ErrorMessage="RequiredFieldValidator" 
            InitialValue="0"></asp:RequiredFieldValidator>
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />

Open in new window

Avatar of GLIanimal
GLIanimal

ASKER

The reason i am using a custom validator is due to this item not being required for certain categories. From what I understand/have found is that I can't say if value in ddl = 'whatever' don't validate for the required field validator.

Also, my field can not equal both, so I can't use an 'AND'. That part works fine, it goes in and checks that and sets the value, as I stated above, but I cant get it to break the page.
PS. It previously was a required field, but the customer needs to be able to add items to the categories that do not require a sub category or series.
ASKER CERTIFIED SOLUTION
Avatar of tiagosalgado
tiagosalgado
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sweet! Thanks. I knew it was something easy I was totally missing