How to set automatically focus on ASP.NET controls when validation fails ?

How to set automatically focus on ASP.NET controls when validation fails ?

If you are using ASP.NET Validation Control then you can use “SetFocusOnError” properties to automatically focus the control if validation fails. This will help the end user to identify the control easily.

To set focus automatically you have to set SetFocusOnError=”True”. By default it’s false. SetFocusOnError of a validation control is associated with “ControlToValidate” properties. If the validation fails, focus will automatically move to the control which is specified in ControlToValidate.

Now in such case where multiple validation fails at same time then first validation will get the focus.

Hope this will help you !

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.