Set Focus on Username text box

Last post 06-25-2009 9:13 AM by Wei Ke. 15 replies.

Sort Posts:

  • Set Focus on Username text box

    01-19-2006, 9:42 AM
    • Member
      315 point Member
    • rgibson69
    • Member since 03-20-2004, 10:43 PM
    • Brantford, Ontario, Canada
    • Posts 64

    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

  • Re: Set Focus on Username text box

    01-19-2006, 1:12 PM
    • Contributor
      2,378 point Contributor
    • jwadsworth
    • Member since 03-13-2003, 4:16 PM
    • Salem, Oregon
    • Posts 542

    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.

  • Re: Set Focus on Username text box

    01-19-2006, 1:38 PM
    • Contributor
      2,378 point Contributor
    • jwadsworth
    • Member since 03-13-2003, 4:16 PM
    • Salem, Oregon
    • Posts 542

    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

  • Re: Set Focus on Username text box

    01-19-2006, 1:57 PM
    • Contributor
      2,378 point Contributor
    • jwadsworth
    • Member since 03-13-2003, 4:16 PM
    • Salem, Oregon
    • Posts 542

    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

  • Re: Set Focus on Username text box

    01-19-2006, 8:17 PM
    • Contributor
      7,239 point Contributor
    • whighfield
    • Member since 01-02-2006, 10:37 PM
    • Winterpeg, Manitoba
    • Posts 1,205
    rgibson69 wrote:

    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

    Please mark the most helpful reply/replies as "Answer".

    - William
    http://thefrozencoder.ca
  • Re: Set Focus on Username text box

    01-20-2006, 12:57 PM
    • Member
      315 point Member
    • rgibson69
    • Member since 03-20-2004, 10:43 PM
    • Brantford, Ontario, Canada
    • Posts 64

    Thanks to both of you. That worked.

    Rob

     

  • Re: Set Focus on Username text box

    01-20-2006, 1:03 PM
    • Member
      315 point Member
    • rgibson69
    • Member since 03-20-2004, 10:43 PM
    • Brantford, Ontario, Canada
    • Posts 64

    Jeremy,

    Nice work on the skins!!

    Rob

     

  • Re: Set Focus on Username text box

    04-05-2007, 7:14 AM
    • Member
      10 point Member
    • ObieOne
    • Member since 04-02-2007, 1:57 PM
    • Posts 14

    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

  • Re: Set Focus on Username text box

    02-27-2008, 11:13 PM
    • Member
      4 point Member
    • nukewarm
    • Member since 02-23-2008, 5:58 AM
    • Posts 2

    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>

  • Re: Set Focus on Username text box

    06-06-2008, 1:06 PM
    • Member
      5 point Member
    • graljackal
    • Member since 05-03-2008, 3:15 PM
    • Posts 6

     This saved me from suicide thanx

  • Re: Set Focus on Username text box

    09-02-2008, 9:01 AM
    • Member
      81 point Member
    • vpmragu
    • Member since 01-09-2008, 6:43 AM
    • Bangalore
    • Posts 28

    thanx nukewarm.

    i got simple and easiest way man.

    once again thank you ver much.

     

    RaguNathan Maniraj,
    Software Engineer,
    Bangalore.
  • Re: Set Focus on Username text box

    10-07-2008, 2:54 AM
    • Member
      2 point Member
    • yupa
    • Member since 10-07-2008, 6:50 AM
    • Posts 1
    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.

  • Re: Set Focus on Username text box

    04-17-2009, 10:54 AM
    • Member
      6 point Member
    • Wei Ke
    • Member since 04-17-2009, 10:53 AM
    • Posts 3

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

  • Re: Set Focus on Username text box

    06-24-2009, 11:43 PM
    • Member
      81 point Member
    • vpmragu
    • Member since 01-09-2008, 6:43 AM
    • Bangalore
    • Posts 28

    hi wei ke,

    Use th following. it works.

    protected void Page_Load(object  sender, EventArgs e)

    {

    Login1.Username.Focus = True;

    }


    RaguNathan Maniraj,
    Software Engineer,
    Bangalore.
  • Re: Set Focus on Username text box

    06-24-2009, 11:46 PM
    • Member
      81 point Member
    • vpmragu
    • Member since 01-09-2008, 6:43 AM
    • Bangalore
    • Posts 28

    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>

    RaguNathan Maniraj,
    Software Engineer,
    Bangalore.
Page 1 of 2 (16 items) 1 2 Next >