Form Validation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have a form which displays some orders based on dates selected or based on
an order number. I have two different buttons for each kind of query, but
both buttons call the same method in the code behind page.
I'm validating the dates and if they're not ok, I set args.IsValid=false;
In the method that retrieves the orders, I check if Page.IsValid.
This way if the dates are not valid, the retrieval by dates is not working,
but so is the retrieval by order number. Even if I would have two different
methods, I would still check Page.IsValid and it would not work.
Is there a way to solve this problem, meaning the “get order by date†button
to depend on the validity of dates text boxes and the validity of the “get
order by order number†button to depend on the validity of the order number
text box?

Thank you.
 
Try setting CausesValidation="False" then in your code behind on the
respective button click use Page.FindControl("controlid") to get the
instance of the specific validator you want, call Validate on it then check
the IsValid property.

MattC
 
Thank you for your reply,
That almost worked. The only problem is that after I validate a control,
even if I set args.IsValid=false in the validation method, and then use
Page.IsValid in the main method, it says that I cannot use Page.IsValid
because I did not use Page.Validate() and because Button.CausesValidation =
"False".

Also, I don't know if this matters, but I have a CustomValidator and a
RequiredFieldValidator assigned to the TextBox controls for dates and a
RequiredFieldValidator assigned to the TextBox controlfor the Order#.

Thank you again
 
You dont need to validate the entire page as this will attempt to validate
every validator control on the page. As I said, on the button click no
validation is called.

Therefore you call validate ONLY on the validators you want to fire. Once
you have called validate you do something like this:

if(_validator1.IsValid() && _validator1.IsValid())
{
//do some stuff
}

In your case I dont think you need to validate the page as you dont want
everything to be checked only that which it related to your button click.

MattC
 
Back
Top