Validators not preventing postbacks

  • Thread starter Thread starter AD
  • Start date Start date
A

AD

Hi,

I have 2 different web pages, both using RequiredFieldValidator's, on
the one page Postbacks are prevented when required fields are blank,
on the other page the validators display the error messages after the
postback has returned, but neither the client side, nor the server
side processing is suppressed (my incomplete data is written to my
table)

Any idea what could be wrong?
 
Hi there,

First if you want to prevent the post from the client browser set
EnableClientScript to true. Second, we can't rely on client script validation
as it's very easy to prepare a page that simply skips it. Anyway, the problem
is you don't check IsValid property before executing database insertion code.
Valdators do not prevent page execution automatically (apart from client
side), therefore before you perfom any action, you need to check if page
passed validation (in other words, if all the validators are valid):

protected void MyInsertButtonClick(object sender, EventArgs e)
{
if (this.IsValid)
{
// insert data to database
}
}

Hope this helps
 
Hi Milosz,

Thanks for the response.

Both EnableClientScript and Enabled is true on the Validator control,
on both pages, on the one page the postback is prevented, on the other
it is not, anything else I can check?
 
check that the db update control has the "CausesValidation" property set to True.

Check the validation group, the db update button(or what ever control you are using) and validation should have the same validation group or they both should have no validation if required.

if non of the bove worked use Page.Validate() in the click event for the db update button, but u don't need to do that if "CausesValidation" = True.
Hi Milosz,

Thanks for the response.

Both EnableClientScript and Enabled is true on the Validator control,
on both pages, on the one page the postback is prevented, on the other
it is not, anything else I can check?
 
Make sure you check the button has got CausesValidation set to true, and the
code tests for IsValid in button's event handler:

if (Page.IsValid)
{
// collect values and insert into a databse
}

hope this helps
 
Back
Top