Novice Developer Novice Developer - 3 months ago 9
ASP.NET (C#) Question

How to use SetFocus on text box control?

I am trying to setfocus on text box.

SetFocus(txtUserName);


I am not seeing any change on web page.

what is a difference between .focu() and setfocus() and what is the functionality of each?

Answer

You can use .Focus() on the control itself, or call the Page's SetFocus(), 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.

Comments