How to Focus on the Control when Validator control fires in ASP.Net
It will be good and user friendnly if we focus the input control whenever the validator controls fires with error message which will let the user know when the input screen has lots of fields. Moving forward, this little code snippet will help us to do that.
All the validator controls(RequiredFieldValidator, RangeValidator, RegularExpressionValidator and CompareValidator control) have a boolean property called SetFocusOnError which can be set true to do this easily. The below does that,
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="Name is Mandatory."
The above code will focus the control specified in ControlToValidate property whenever the validation fails.
This small feature will help the user to find the control that raised the validation message very easily when there are large numbers of input controls in the page.