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
anD666
  • 591
  • 3
  • 8
  • 24

9 Answers9

31

I'm using Page.Form.DefaultFocus and it works:

// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;
Hoang Tran
  • 481
  • 1
  • 6
  • 13
11

Are you using a ScriptManager on the Page? If so, try the following:

public void SetInputFocus()
{
    TextBox tbox = this.loginForm.FindControl("UserName") as TextBox;
    if (tbox != null)
    {
       ScriptManager.GetCurrent(this.Page).SetFocus(tbox);
    }
}

Update: Never used a multiview before, but try this:

protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
   SetInputFocus();
}
djdd87
  • 63,008
  • 24
  • 149
  • 191
  • @Aristos - Thanks for pointing that out, I just copied and pasted my own SetInputFocus method and forgot to change it for this question. – djdd87 Jun 15 '10 at 09:28
  • Still nothing, it is getting to the `ScriptManager.GetCurrent(this.Page).SetFocus(tbox);` statement but still doesnt set the focus – anD666 Jun 15 '10 at 09:32
  • @anD666 - Updated my answer, give it a try. Just a guess, so it's untested. I just robbed the idea of a google search result. – djdd87 Jun 15 '10 at 09:54
  • I think it is a problem with the master page. I tried copying the login control into a clean page and your code worked fine. Using the master page it doesnt seem to find the form with runat="server". I think I will remove it from a master page. Thank you – anD666 Jun 15 '10 at 10:26
  • The script manager wasnt on the master page, I have tried moving it onto that and it still cannot find a form with the tag runat=server. This form is on the master page and i cant add another to the webform. – anD666 Jun 15 '10 at 11:00
1
protected void Page_Load(object sender, EventArgs e)
{
    SetFocus(LoginCntl.FindControl("UserName"));
}
Ullas
  • 10,785
  • 4
  • 28
  • 46
Rekha
  • 11
  • 1
  • 3
    When the page loads, the cursor is positioned in the username textbox. It works out. Please try it. – Rekha Jan 28 '11 at 01:05
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

marisks
  • 1,708
  • 1
  • 15
  • 32
0
protected override void OnPreRender(EventArgs e)
{
        base.OnPreRender(e);
        Login.FindControl("UserName").Focus();

}
Mario
  • 325
  • 3
  • 11
0

My problem arrized when i moved login control to a custom control and tried to find UsernameTextBox at the OnInit() method. OnInit of a control is executed before OnInit of Page and this is why no Form control have been created.

I moved the call to UsernameTextBox to the OnLoad function and it worked correctly.

Menelaos Vergis
  • 3,081
  • 2
  • 25
  • 39
0

You may try to do the following:

-Register two scripts (one to create a function to focus on your texbox when page is displayed, second to register id of the textbox)

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "on_load", 
                "<script>function window_onload() \n { \n if (typeof(idLoginTextBox) == \"undefined\" || idLoginTextBox == null) \n return; \n idLoginTextBox.focus();\n } \n window.onload = window_onload; </script>");

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Focus", String.Format("<script>var idLoginTextBox=document.getElementById(\"{0}\").focus();</script>", this.loginForm.ClientID));             

As the result you should get the following in your code:

      <script>
          function window_onload()
          {
            if (typeof(idLoginTextBox) == "undefined" || idLoginTextBox == null)
                return;     
            idLoginTextBox.focus();
        }
        window.onload = window_onload;     
      </script>   



<script>
        var idLoginTextBox=document.getElementById("ctl00_LoginTextBox").focus();
  </script>
Konrad
  • 548
  • 1
  • 5
  • 16
0

None of the above answers worked for me, so I simply tried:

protected void Page_Load(object sender, EventArgs e) {
    // This works for me
    TxtMyTextBoxName.Focus();
}

... and it worked!
With an ASP TextBox defined as:

<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox>
LePatay
  • 144
  • 1
  • 8
0

I've been struggling with this too and I've found a solution that seems to work very well even with deeply nested controls (like AspDotNetStorefront a.k.a. ASPDNSF uses). Note the following code called from the Page_PreRender routine. I knew the name of the TextBox I wanted to give focus to and so I just called FocusNestedControl(Me, "UserName"). I just used Me here because all the routine needs is a parent of the control to get focus; it doesn't matter which parent.

    Public Function FocusNestedControl(ByVal ParentControl As Control, ByVal ControlNameToFocus As String) As Control

        If ParentControl.HasControls Then
            For Each childCtrl As Control In ParentControl.Controls
                Dim goodCtrl As Control = FocusNestedControl(childCtrl, ControlNameToFocus)
                If goodCtrl IsNot Nothing Then
                    goodCtrl.Focus()
                    Return goodCtrl
                End If
            Next
        Else
            If ParentControl.ID = ControlNameToFocus Then
                ParentControl.Focus()
                Return ParentControl
            End If
        End If

        Return Nothing
    End Function
cjbarth
  • 3,512
  • 5
  • 37
  • 55