I have a form that inserts a record into database. The form has two required fields with RequiredFieldvalidators assigned to them and an insert button. After Insert button is clicked, entry is added to DB and textboxes are cleared. But when the page is reloaded after postback, validation kicks in. Is there a way to set page as valid after postback?
Validators are needed to validate data before a postback, so after postback the page is have to be valid.
Update. If record has been added to the database after insert button click - this means that page was valid. Page.IsValid is used for checking in the server-side in several cases and client-side validation has to be occured.
In your button click event, do this:
if (this.IsValid)
{
// save operation
}
I sounds like you might not be using the validators correctly. You cannot rely on client-side validation to ensure your inputs are ok - you need to validate on both the client and the server side. If the user turns off javascript, then there will be no client-side validation at all, so if you're not checking on the server as well, you'll just be inserting their raw input into the database.
Validation messages should be showing up after the postback to tell you when there was invalid inputs - that's their main function. Client-side messages that occur pre-post are just a convenience for javascript enabled browsers.
Before you insert anything into the database, you need to be checking all the validators by using this pattern:
if (Page.IsValid)
{
// Insert into database
}
If the validators are not valid, then the page will return without inserting anything, and show the validator messages. If everything is indeed valid, then the messages won't be showing up - but regardless of what code you run, the messages will show up if the inputs were invalid.