CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to Focus on the Control when Validator control fires in ASP.Net ?
Submitted By Bala Murugan
On 4/12/2010 8:55:41 AM
Tags: ASP.Net,CodeDigest  

How to Focus on the Control when Validator control fires in ASP.Net

 

Validator controls packed in ASP.Net control set are extremly useful control since it just satisfies most of our common application validation needs. This validation control will work on the client side using the inbuilt javascript libraries emitted by the validator controls. These validation controls can be customized heavily to satisfy most of our needs. One such  thing is focussing the input control when the validation attached to it is failed. 

 

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."
        SetFocusOnError="True"></asp:RequiredFieldValidator>

 

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.

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..