How to set focus to the Username on the Asp.net Login Control
C#, asp.net July 30th, 2006[csharp]
protected void Page_Load(object sender, EventArgs e)
{
TextBox usernameControl = Login1.FindControl(”UserName”) as TextBox; // Change Login1 to the name of your Login Control
if (usernameControl != null)
Page.SetFocus(usernameControl);
}
[/csharp]
January 21st, 2007 at 4:32 pm
How about doing this way?
if (Login1.FindControl(”UserName”) != null)
((TextBox)Login1.FindControl(”UserName”)).Focus();
I don’t know whether we are doing anything different but I thought this should work too. My $0.02!
January 22nd, 2007 at 9:31 am
Yes you can do it tha way, but it’s not as efficient.
Generally, for perfomrance increase (I know it’s not that important in this case) you should assign the check to a variable and check if the variable is null.
In you example, the findcontrol, which has to iterate the list of all the controls on the page, has to be run twice if the control exists.
It’s just a habit you should get into, doing the first way, to keep performance of you application to the optimum, both are attain the same result.
March 15th, 2007 at 2:36 pm
Thank you so much dude… It helps me a lot. if I didn’t find this then I would be still doing the old way of doing this.
January 16th, 2008 at 11:29 am
Suppose my login controls name is userLogin. Then inside Page_Load if we add the following code:
userLogin.FindControl(”UserName”).Focus();
Whether this is good?