且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-19 13:52:03

您的网页上使用一个ScriptManager?如果是这样,请尝试以下操作:

 公共无效SetInputFocus()
{
    文本框TBOX = this.loginForm.FindControl(用户名)的文本框;
    如果(TBOX!= NULL)
    {
       ScriptManager.GetCurrent(this.Page).SETFOCUS(TBOX);
    }
}

更新:之前从未使用过的多视点,但试试这个:

 保护无效MultiView1_ActiveViewChanged(对象发件人,EventArgs的发送)
{
   SetInputFocus();
}

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

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