我正在尝试将焦点设置为ASP.NET登录控件中的用户名TextBox。
我试图通过几种方式做到这一点,但似乎没有一种方法可行。页面正在加载但不会进入控件。
这是我尝试过的代码。
SetFocus(this.loginForm.FindControl("UserName"));
和
TextBox tbox = (TextBox)this.loginForm.FindControl("UserName");
if (tbox != null)
{
tbox.Focus();
} // if
答案 0 :(得分:29)
我正在使用Page.Form.DefaultFocus并且它可以工作:
// inside page_load, LoginUser is the Login control
Page.Form.DefaultFocus = LoginUser.FindControl("Username").ClientID;
答案 1 :(得分:10)
您是否在页面上使用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();
}
答案 2 :(得分:1)
protected void Page_Load(object sender, EventArgs e)
{
SetFocus(LoginCntl.FindControl("UserName"));
}
答案 3 :(得分:0)
您可以尝试执行以下操作:
- 注册两个脚本(一个用于创建在显示页面时专注于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>
答案 4 :(得分:0)
我一直在努力解决这个问题,我发现一个解决方案,即使使用深度嵌套控件(如AspDotNetStorefront a.k.a. 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
答案 5 :(得分:0)
您可以直接在LoginControl上设置焦点,它会自动将焦点设置在控制中的第一个字段上。在你的情况下:
this.loginForm.Focus();
有关MSDN的更多信息:How to: Set Focus on ASP.NET Web Server Controls
答案 6 :(得分:0)
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Login.FindControl("UserName").Focus();
}
答案 7 :(得分:0)
当我将登录控件移动到自定义控件并尝试在OnInit()方法中找到UsernameTextBox时,我的问题变得很复杂。 在OnInit of Page之前执行控件的OnInit,这就是为什么没有创建Form控件的原因。
我将对UsernameTextBox的调用移动到OnLoad函数,它运行正常。
答案 8 :(得分:0)
上述答案都不适合我,所以我只是尝试了:
const withMutations = graphql(CommentMutation, {
options: () => ({
...getDefaultOptions('collaboration')
}),
props: ({ ownProps, mutate }) => {
return {
submitComment: ({ value }) =>
mutate({
variables: {
commentData: {
comment: value
}
},
optimisticResponse: {
__typename: 'Mutation',
addComment: {
__typename: 'CommonResponse',
_id : -1,
id : null,
comment : value,
meta : ownProps.data[0].meta,
owner : ownProps.data[0].owner,
status : 'success',
message : 'Comment successfully added.',
action : 'AddComment',
},
},
// optimisticResponse: {
// __typename: 'Mutation',
// addComment: {
// __typename: 'CommonResponse',
// id : -1,
// status : 'success',
// message : 'Comment successfully added.',
// action : 'AddComment',
// },
// },
update: (store, { data }) => {
const data2 = store.readQuery({ query: CommentsQuery });
data2.comments.unshift(data.addComment);
store.writeQuery({ query: CommentsQuery, data: data2 });
},
}),
};
},
});
......它奏效了! 使用ASP TextBox定义为:
protected void Page_Load(object sender, EventArgs e) {
// This works for me
TxtMyTextBoxName.Focus();
}