locked
Set Focus on Username text box RRS feed

  • Question

  • User-554730672 posted

    I have modified the login on the main page slightly. I deleted the PWS login and replaced it with one of the default Login controls in VWD. See www.robgibson.ca . What I would like is to have the default focus set to the username text box within the control. I have tried a few of the solutions presented in the posts here but I can't seem to get anything to work.

    Suggestions?

    Rob

    Thursday, January 19, 2006 9:42 AM

All replies

  • User595806616 posted

    Rob,

    Are you using a LoginView control?

    That's what I'm using. I tried this:

    Dim tmpTextbox As New TextBox

    tmpTextbox = CType(LoginArea.FindControl("UserName"), TextBox)

    If Not tmpTextbox Is Nothing Then

    tmpTextbox.Focus()

    End If

    testBox.Focus()

    The focus wont set to the textbox inside the loginview control.

    However, the focus sets fine in the textbox outside the control.

    I'm sure it can be done through javascript. I look at it a bit.

    Thursday, January 19, 2006 1:12 PM
  • User595806616 posted

    Rob,

    This works for me. I'm sure there is a better way to do this by looking up the textbox id first rather than hard coding what we view in the page source. View the source in the browser to see the name of the control that asp.net uses, plug it in to the getElementById, and give it a try.

    ' Initialize a stringbuilder object, much faster than string concatenation

    ' Do not do any string concatenations within the string builder or it defeats the purpose.

    Dim scriptLoader As New System.Text.StringBuilder()

    With scriptLoader
       .Append(
    "<script type='text/javascript'>")
       .Append(vbCrLf)
       .Append(
    "var txtBox=document.getElementById('ctl00_Main_LoginArea_Login1_UserName');")
       .Append(vbCrLf)
       .Append("if (txtBox!=null ) txtBox.focus();")
       .Append(vbCrLf)
       .Append("</script>")

       ' Register script with page 
       Me.ClientScript.RegisterStartupScript(Me.GetType(), "onLoadCall", .ToString())

    End With

    Thursday, January 19, 2006 1:38 PM
  • User595806616 posted

    I should of got this right the first time. Here is probably the best way.

    Dim tmpTextbox As New TextBox
    Dim tmpLogin As New System.Web.UI.WebControls.Login

    tmpLogin = CType(LoginArea.FindControl("Login1"), System.Web.UI.WebControls.Login)

    If Not tmpLogin Is Nothing Then
       tmpTextbox = CType(tmpLogin.FindControl("UserName"), TextBox)
    End If

    If Not tmpTextbox Is Nothing Then
       tmpTextbox.Focus()
    End If

    Thursday, January 19, 2006 1:57 PM
  • User1052258516 posted

    I have modified the login on the main page slightly. I deleted the PWS login and replaced it with one of the default Login controls in VWD. See www.robgibson.ca . What I would like is to have the default focus set to the username text box within the control. I have tried a few of the solutions presented in the posts here but I can't seem to get anything to work.

    Suggestions?

    Rob

    This is why I so love ASP.NET 2.0.  LoginForm is the id of the asp:login control and UserName is the name of the textbox in that control.  You should probably check to see if either the login or txt vars are nothing before you try to do the SetFocus.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim login As System.Web.UI.WebControls.Login = LoginArea.FindControl("LoginForm")

    Dim txt As TextBox = login.FindControl("UserName")

    Page.SetFocus(txt)

    End Sub

    Thursday, January 19, 2006 8:17 PM
  • User-554730672 posted

    Thanks to both of you. That worked.

    Rob

     

    Friday, January 20, 2006 12:57 PM
  • User-554730672 posted

    Jeremy,

    Nice work on the skins!!

    Rob

     

    Friday, January 20, 2006 1:03 PM
  • User1541959197 posted

    I had to translate  it so it would work with masterpage and codebehind.

    I came across this. Works great. Now even the default button is set if you press enter.

     

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    SetFocus(Login1)

    Dim ctl As Control = Login1.FindControl("LoginButton")

    Login1.Attributes.Add("onkeypress", String.Format("javascript:return WebForm_FireDefaultButton(event, '{0}')", ctl.ClientID))

    End Sub

     

    GoodLuck

    Arno

    Thursday, April 5, 2007 7:14 AM
  • User-950799310 posted

    This is what I use for a c# page.

    <script runat="server"> 

    protected void Page_Load(object sender, EventArgs e){

               SetFocus(Login1.FindControl("UserName"));

    </script>

    Wednesday, February 27, 2008 11:13 PM
  • User988255519 posted

    thanx nukewarm.

    i got simple and easiest way man.

    once again thank you ver much.

     

    Tuesday, September 2, 2008 9:01 AM
  • User262177051 posted
    protected void Page_Load(object sender, EventArgs e)

    {

    Login1.Focus();

    }

    The focus will be put on the first eligible control within the Login control - UserName textbox.

    Tuesday, October 7, 2008 2:54 AM
  • User-1922881531 posted

    I try this but it seems it doesn't set the focus in the UserName textbox.

    Friday, April 17, 2009 10:54 AM
  • User988255519 posted

    hi wei ke,

    Use th following. it works.

    protected void Page_Load(object  sender, EventArgs e)

    {

    Login1.Username.Focus = True;

    }


    Wednesday, June 24, 2009 11:43 PM
  • User988255519 posted

    you can use this also.

    This is what I use for a c# page.

    <script runat="server"> 

    protected void Page_Load(object sender, EventArgs e){

               SetFocus(Login1.FindControl("UserName"));

    </script>

    Wednesday, June 24, 2009 11:46 PM
  • User-1922881531 posted

     THanks RaguNathan. I figured.

    Thursday, June 25, 2009 9:13 AM
  • User-976313516 posted

     This works for me:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!User.Identity.IsAuthenticated)
                {
                Login lg = this.Login1;
                    TextBox tb = (TextBox)lg.FindControl("UserName");
                    if (tb != null)
                    {
                        SetFocus(tb);
                    }
                }
            }
        }
    


     

    Friday, January 22, 2010 10:44 AM
  • User-492525475 posted

    hi,

    I'm using two text boxes for allowing user to enter two  four digit numbers. After entering first 4 digit the focus must change to the second text box. Can u please help me with this issue

    Friday, April 16, 2010 5:15 PM
  • User-1921245745 posted

    Hi All,

    I have tried to set focus by all approaches you have suggested and all I know (most of are same). Problem here is focus is set to textbox but after that it lost. May be due to other process happen after main page load like load master page, load user controls.

    I have used both master pages and user controls.

    Please suggest any way so that I can retain the set focus in the text box.

    Friday, September 10, 2010 10:48 AM
  • User1451727638 posted

    Or

    Protected Sub Login1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)

    sender.Focus()

    End Sub

    Friday, October 29, 2010 9:39 AM
  • User-1368651969 posted

    can you try control.focus() method

    ex:

    Login1.Focus();

    Wednesday, March 30, 2011 1:24 AM
  • User1177242335 posted

    Suppose txtUserName is your Username textbox name

    than try following code

    txtUserName.Focus();

    Thursday, April 14, 2011 1:44 PM