views:

230

answers:

4

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?

+2  A: 
 txtUserName.Focus()

http://msdn.microsoft.com/en-us/library/ms178232%28VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.control.focus%28VS.80%29.aspx

John Sheehan
what is a difference between .focu() and setfocus()and what is the functionality of each?
Novice Developer
+1  A: 

Controls have a Focus() method which you can call to set the focus to that control.

txtUserName.Focus();

Additionally, the Page has a SetFocus() method, which takes either a Control object or the ID of the Control you want to set focus to.

this.Page.SetFocus(txtUserName);

or

this.Page.SetFocus(txtUserName.ClientID);
Dave Bauman
Are both same functionally?
Novice Developer
+1  A: 

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

http://msdn.microsoft.com/en-us/library/e04ah0f4.aspx

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.

http://msdn.microsoft.com/en-us/library/ms178232%28VS.80%29.aspx

Joshua Tompkins
+3  A: 

Depending on how you want your workflow to be, or what your specs are, you have 2 options:

  1. Set if from server-side. Like John mentioned you can use the Focus() method available to UI controls. This will apply the focus to the control when your page will render in the browser. However, as a result of user interaction with other elements on the page you will loose it.

  2. Set it on the client-side using JavaScript. This will give you more flexibility as you can apply the focus based on user interaction. To apply the focus on the client side, you can use the DOM (Document Object Model). You need to get a reference to the control and then you can call the focus() method:

    document.getElementById(txtUserName).focus();

That is if the ID of the control (as it's registered on the client side) is txtUserName. Depending on the layout of your page this will be less likely the case, and you might get into troubles if you change the layout, so this is not really the best practice out there.

A good practice is to register a JavaScript variable whose purpose is to hold the client ID of the control as it will be rendered in the HTML. You can accomplish this by using the ClientScriptManager class:

ClientScript.RegisterClientScriptBlock(this.GetType(), "setFocus", "var focusFieldId = '" + txtUserName.ClientID + "';", true);

and then on the client side you can say

document.getElementById(focusFieldId).focus();

You could refactor this into a method called setUserNameFocus() for instance and then call that method on whatever client-side event you want. One popular practice is for instance to keep the focus on a certain field when the user clicks on a location on the page. For instance your HTML body element could look like this if you don't want to ever loose the focus for your element:

<body onload="setUserNameFocus();" onclick="setUserNameFocus();">
Mircea Grelus
The link to the ClientScriptManager is the wrong one. It points to the obsolete API. I get an error when trying to edit the answer. I'll try to edit it later on. In the mean time, the link to the correct reference is this:http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx
Mircea Grelus