在页面加载时将焦点设置为ASP.NET登录控件中的文本框

时间:2022-07-28 16:49:26

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

我试图将焦点设置为ASP.NET登录控件内的用户名TextBox。

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

9 个解决方案

#1


9  

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

你在页面上使用ScriptManager吗?如果是这样,请尝试以下操作:

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();
}

#2


28  

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

我正在使用Page.Form.DefaultFocus并且它可以工作:

// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;

#3


1  

protected void Page_Load(object sender, EventArgs e)
{
    SetFocus(LoginCntl.FindControl("UserName"));
}

#4


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)

- 注册两个脚本(一个用于创建在显示页面时专注于texbox的功能,第二个用于注册文本框的ID)

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>

#5


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.

我一直在努力解决这个问题,我发现了一个解决方案,即使使用深度嵌套控件(如AspDotNetStorefront a.k.a. ASPDNSF使用),它似乎也能很好地工作。请注意从Page_PreRender例程调用的以下代码。我知道我想要关注的TextBox的名称,因此我只调用了FocusNestedControl(Me,“UserName”)。我刚刚在这里使用了我,因为所有常规需求都是控件的父级,以获得焦点;哪个家长都没关系。

    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

#6


0  

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

您可以直接在LoginControl上设置焦点,它会自动将焦点设置在控制中的第一个字段上。在你的情况下:

this.loginForm.Focus();

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

有关MSDN的更多信息:如何:重点关注ASP.NET Web服务器控件

#7


0  

protected override void OnPreRender(EventArgs e)
{
        base.OnPreRender(e);
        Login.FindControl("UserName").Focus();

}

#8


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.

当我将登录控件移动到自定义控件并尝试在OnInit()方法中找到UsernameTextBox时,我的问题变得很复杂。在OnInit of Page之前执行控件的OnInit,这就是为什么没有创建Form控件的原因。

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

我将对UsernameTextBox的调用移动到OnLoad函数,它运行正常。

#9


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定义为:

<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox>

#1


9  

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

你在页面上使用ScriptManager吗?如果是这样,请尝试以下操作:

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();
}

#2


28  

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

我正在使用Page.Form.DefaultFocus并且它可以工作:

// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;

#3


1  

protected void Page_Load(object sender, EventArgs e)
{
    SetFocus(LoginCntl.FindControl("UserName"));
}

#4


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)

- 注册两个脚本(一个用于创建在显示页面时专注于texbox的功能,第二个用于注册文本框的ID)

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>

#5


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.

我一直在努力解决这个问题,我发现了一个解决方案,即使使用深度嵌套控件(如AspDotNetStorefront a.k.a. ASPDNSF使用),它似乎也能很好地工作。请注意从Page_PreRender例程调用的以下代码。我知道我想要关注的TextBox的名称,因此我只调用了FocusNestedControl(Me,“UserName”)。我刚刚在这里使用了我,因为所有常规需求都是控件的父级,以获得焦点;哪个家长都没关系。

    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

#6


0  

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

您可以直接在LoginControl上设置焦点,它会自动将焦点设置在控制中的第一个字段上。在你的情况下:

this.loginForm.Focus();

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

有关MSDN的更多信息:如何:重点关注ASP.NET Web服务器控件

#7


0  

protected override void OnPreRender(EventArgs e)
{
        base.OnPreRender(e);
        Login.FindControl("UserName").Focus();

}

#8


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.

当我将登录控件移动到自定义控件并尝试在OnInit()方法中找到UsernameTextBox时,我的问题变得很复杂。在OnInit of Page之前执行控件的OnInit,这就是为什么没有创建Form控件的原因。

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

我将对UsernameTextBox的调用移动到OnLoad函数,它运行正常。

#9


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定义为:

<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox>