Setting the focus automatically on the control in a case when validation gets failed in asp.net.

Most of the time we are making use of validation control for validating the controls like RequiredFieldValidator, RangeValidator etc.

So, basically what happens is, when end-users are using our web-form and submitting the details by clicking the button then if validation fails then we can easily send the focus to that particular control so that it can be identified easily by the end-user.

We can do this by setting the “SetFocusOnError”  property of the validation control to true. By default it is set to false.

Untitled.png

In the above case if the validation will fail then automatically the focus will go to the TextBox1 because i have set the SetFocusOnError="true" and TextBox1 is getting validated here.

Next Recommended Reading Use of Validation Group in asp.net