Validators not preventing postbacks

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?
 
G

Guest

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
 
A

AD

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?
 
T

Toty Santana

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?
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top