Monday, March 30, 2009

How to set focus on control using javascript

This article demonstrates how to set focus to an ASP.NET Web Form control by using client-side script.

ASP.NET Web Form controls provide a similar look and feel of the traditional HTML controls while they provide a consistent and structured interface and more robust features. In addition, you can use client-side scripting to enhance the functionality that these controls provide.

In my project, i required to set focus on particular control on the drop down change event. For this i used/added following javascript on the SelectedIndexChanged event of drop down list:

Page.RegisterStartupScript("MoveVerticalScrollBar", "<script>document.all('" + chkEmployee.ClientID + "').focus();</script>");

With the help of above script, focus set to
chkEmployee control after the SelectedIndexChanged event of drop down list.


About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010. He is an contributor to MSDN and Amit have experience in SharePoint,ASP.Net with C#, MVC, Sitecore, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.