2

I'm using the asp.net Login control:

 <asp:Login ID="Login2" runat="server" LoginButtonType="Link"
                Width="280px" Height="150">               
            </asp:Login>

How could I set the Username textbox to have the focus when initially the page loads and also the Login link button should be clicked when the user enters.

I tried the below in the Page_Load event but didn't work?

  this.Login2.FindControl("Username").Focus();
  this.form1.DefaultButton = this.Login2.FindControl("LoginButton").UniqueID;

Thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258

4 Answers4

3

Try something like this:

TextBox tb = (TextBox)Login1.FindControl("UserName");
tb.Focus();
Burt
  • 7,680
  • 18
  • 71
  • 127
1
if (!HttpContext.Current.User.Identity.IsAuthenticated) {
   Login lg = (WebControls.Login)LoginView1.FindControl("Login2");
   TextBox tb = (TextBox)lg.FindControl("UserName");
   tb.Focus();
}

Have a look here to see how you can define the login-button as default button in Login:

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
protected void Page_Load(object sender, System.EventArgs e)
{
    System.Web.UI.WebControls.Login login = LoginArea.FindControl("LoginForm");

    TextBox txt = login.FindControl("UserName");

    Page.SetFocus(txt);

}
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • ok then please give a look into this post http://stackoverflow.com/questions/3043592/set-focus-to-textbox-in-asp-net-login-control-on-page-load – rahularyansharma Oct 17 '11 at 09:52
0

You can simply write Login1.Focus() at page load event and also basically, you can define a DefaultButton not just on the Form level, but also on individual Panel level, as long as the focus is within the panel, the default button for the panel will be used if you hit "Enter