Validators firing for wrong button click?

I had a query from user complaining that his validators are firing for the click of a wrong button. I provided a work around solution for his problem which I thought would be worthwhile to share with all of you.

All of us would agree that ASP.NET Validation Controls are a great means of quickly developing user input based forms and enforcing validations for the entries.

Going back to classic ASP, one has to know JScript/Javascript and worry about whether scripts are enabled etc., for developing a user input form. Well thats pretty old now since .NET has been there for more than 4 years now. So lets get into the actual scenario.

Say in an ASPX Page you have the following controls:-

TextBox1, TextBox2 - Two Textboxes
Button1, Button2 - Two Buttons
RequiredFieldValidator1, RequiredFieldValidator2 - Two RequiredFieldValidators associated with each Textbox.

Now, once you declare the above and assign the ControlToValidate property to the TextBoxes respectively, you would expect that the first validator should fire only when the TextBox1 doesnt get a valid input and the second validator should fire only when the TextBox2 doesnt get a valid input. That is, when you click Button1 which should validate the TextBox1, you should get the validation error only if TextBox1 is empty and similarly if you click Button2 you should get the validation error only if TextBox2 is empty.

However, when you click any of the buttons with either one or both the TextBoxes not meeting the criteria, the form cannot be submitted.

The reason is that, the Button (asp:button) is a server control and it posts back to the server. However, before the page is posted, the validity of the page would be checked and if one of the two validations are not met, the page is not posted.

The workaround is as follows:-

1. On the page_load event of the page set the Enabled property of the second validator i.e. RequiredFieldValidator2 to false, as follows:-

RequiredFieldValidator2.Enabled = false

2. This would ensure that when Button1 is clicked, the second validator doesnt fire.

3. Then, on the Button click event of Button1, set back the Enabled property of the second validator to True.

RequiredFieldValidator2.Enabled = true

4. Now, the second validator would fire if the TextBox2 is left empty and the page is tried to be posted.

5. Similarly you can also enable and disable other validation controls.

Well, this is a specific requirement for which I have provided the solution and may not be a generic solution. However, I am exploring other options and in the meanwhile if any one has a good pointer other than using Third party validation controls, please do let me know.

This article applies to .NET 1.x versions only.

Print | posted on Thursday, May 12, 2005 10:58 AM

Comments on this post

# re: Validators firing for wrong button click?

Requesting Gravatar...
Thank you for this solution. It makes sense and fixes the problem.
Left by Douglas on Mar 27, 2017 4:16 PM

Your comment:

 (will show your gravatar)