我试图将焦点设置在 ASP.NET 登录控件内的用户名 TextBox 上。
我尝试了几种方法,但似乎都没有。页面正在加载,但未转到控件。
这是我尝试过的代码。
SetFocus(this.loginForm.FindControl("UserName"));
和
TextBox tbox = (TextBox)this.loginForm.FindControl("UserName");
if (tbox != null)
{
tbox.Focus();
} // if
我正在使用 Page.Form.DefaultFocus 并且它有效:
// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;
您在页面上使用 ScriptManager 吗?如果是这样,请尝试以下操作:
public void SetInputFocus()
{
TextBox tbox = this.loginForm.FindControl("UserName") as TextBox;
if (tbox != null)
{
ScriptManager.GetCurrent(this.Page).SetFocus(tbox);
}
}
更新:以前从未使用过多视图,但试试这个:
protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
{
SetInputFocus();
}
protected void Page_Load(object sender, EventArgs e)
{
SetFocus(LoginCntl.FindControl("UserName"));
}
您可以尝试执行以下操作:
-注册两个脚本(一个是创建一个函数来在页面显示时关注你的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));
结果,您应该在代码中获得以下内容:
<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>
我也一直在为此苦苦挣扎,我找到了一个解决方案,即使使用深度嵌套的控件(如 AspDotNetStorefront aka ASPDNSF 使用),它似乎也能很好地工作。请注意从Page_PreRender例程调用的以下代码。我知道我想要关注的 TextBox 的名称,所以我只调用了FocusNestedControl(Me, "UserName"). 我只是Me在这里使用,因为所有的例行程序都需要一个父控件来获得焦点;哪个父母都没有关系。
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
您可以直接在 LoginControl 上设置焦点,它会自动将焦点设置在控件的第一个字段上。在你的情况下:
this.loginForm.Focus();
有关 MSDN 的更多信息:如何:将焦点放在 ASP.NET Web 服务器控件上
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Login.FindControl("UserName").Focus();
}
当我将登录控件移动到自定义控件并尝试在 OnInit() 方法中查找 UsernameTextBox 时,我的问题出现了。控件的 OnInit 在 Page 的 OnInit 之前执行,这就是没有创建 Form 控件的原因。
我将对 UsernameTextBox 的调用移至 OnLoad 函数,它工作正常。
以上答案都不适合我,所以我只是尝试:
protected void Page_Load(object sender, EventArgs e) {
// This works for me
TxtMyTextBoxName.Focus();
}
......它奏效了!
将 ASP TextBox 定义为:
<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox>
您必须将文本框放在 ASP.NET 上的此代码中
<form id="WHATEVERYOUWANT" runat="server" method="post">
<div>
<!-- Put your code here-->
</div>
</form>