Disabling Form Validation When A Cancel Button Is Clicked

In a form that accepts user inputs, you can use ASP.NET Validation Web Controls to validate user inputs without writing much code. When a user submits the page, the validation controls will check the user submitted information on both client-side and server-side, catch any invalid user inputs, and display error messages to the user. But there is a time that you want your form to be submitted without going through the validation process. For instance, it is very often that a form includes a Cancel button that allows the user to abandon the page without having to fill in valid values. Implementing a Cancel button in a form with the presence of validation controls, however, is more difficult than you might expect. Setting the Validation Controls property IsValid to true wont work because the client-side validation scripts can prevent any subroutine associated with the Cancel button from ever executing. As a result, the validation controls will insist the user to submit valid data before the form can cancel the action. To solution is to use CauseValidation property of the button control to disable validation controls. This property is supported by Button, LinkButton, and ImageButton controls. By setting this property to false for the cancel button control you are using, the validation can be disabled when the button is clicked. It is simple like this:

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish