[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

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

0
GLIanimal
Asked:
GLIanimal
  • 3
  • 2
1 Solution
 
tiagosalgadoCommented:
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

0
 
GLIanimalAuthor Commented:
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.
0
 
GLIanimalAuthor Commented:
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.
0
 
tiagosalgadoCommented:
On your button click event, add this

if (Page.IsValid)
{
    // your code to execute if your page is valid
}

Open in new window

0
 
GLIanimalAuthor Commented:
Sweet! Thanks. I knew it was something easy I was totally missing
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now