I am trying to set the focus on a text box.
SetFocus(txtUserName);
I am not seeing any change on the web page.
What is the difference between .Focus() and SetFocus() and what is the functionality of each?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
.Focus()on the control itself, or call the Page’sSetFocus(), passing in a reference to the control. Note that you need to pass in the control, not the ID, as that doesn’t seem to be supported (at least as of .NET 3.5). See MSDN Page.SetFocus for more.As to the difference between the methods? They’re largely interchangeable, and you should call whichever is more convenient given the objects you have access to – usually calling
.Focus()on the control is easier. See How to Set Focus on ASP.NET Web Server Controls on MSDN.