Alex Rivera | Logout

Set focus to textbox in ASP.NET Login control on page load

Asked 2010-06-15T08:26:29.050
20

I am trying to set the focus to the user name TextBox which is inside an ASP.NET Login control.

I have tried to do this a couple of ways but none seem to be working. The page is loading but not going to the control.

Here is the code I've tried.

SetFocus(this.loginForm.FindControl("UserName"));

And

TextBox tbox = (TextBox)this.loginForm.FindControl("UserName");
if (tbox != null)
{    
  tbox.Focus();
} // if
Edit
Report

1 Answer

0

You can set focus directly on LoginControl and it will automatically set focus on first field in control. In your case:

this.loginForm.Focus();

More info on MSDN: How to: Set Focus on ASP.NET Web Server Controls

answered 2012-08-08T14:55:49.713

Your Answer