2010-06-15 47 views
16

我試圖將焦點設置到用戶名文本框這是一個ASP.NET登錄控件中。將焦點設置到ASP.NET登錄控件文本框在頁面加載

我試圖做這幾種方式,但沒有似乎工作。該頁面正在加載,但不會去控制。

這是我試過的代碼。

SetFocus(this.loginForm.FindControl("UserName")); 

而且

TextBox tbox = (TextBox)this.loginForm.FindControl("UserName"); 
if (tbox != null) 
{  
    tbox.Focus(); 
} // if 
+2


隨着ASP文本框定義。使用運算符'as',否則'(TextBox)null'將拋出'NullReferenceException'。所以你的空檢查將永遠不會達到。 – abatishchev 2010-06-15 08:29:26

+1

你有沒有將你的asp.net登錄控件轉換爲模板? – Aristos 2010-06-15 08:32:22

+0

是的,我已將登錄控件轉換爲模板。 – anD666 2010-06-15 09:13:28

回答

9

你的頁面上使用一個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(); 
} 
+0

@Aristos - 感謝您指出了這一點,我只是複製並粘貼了自己的SetInputFocus方法,並忘記對此問題進行更改。 – GenericTypeTea 2010-06-15 09:28:03

+0

仍然沒有,它是越來越'ScriptManager.GetCurrent(this.Page).SetFocus(tbox);'語句,但仍然沒有設置焦點 – anD666 2010-06-15 09:32:48

+0

@ anD666 - 更新我的答案,試試看。只是一個猜測,所以它沒有經過測試。我剛剛搶劫了谷歌搜索結果的想法。 – GenericTypeTea 2010-06-15 09:54:40

0

您可以嘗試做如下:

-Register兩個腳本(一個用於在頁面顯示時創建一個專注於您的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> 
1
protected void Page_Load(object sender, EventArgs e) 
{ 
    SetFocus(LoginCntl.FindControl("UserName")); 
} 
+3

加載頁面時,光標位於用戶名文本框中。它解決了。請嘗試。 – Rekha 2011-01-28 01:05:48

0

我一直在爲此而努力過,我已經發現,似乎與深層嵌套的控制,即使工作得很好的解決方案(像ASPDotNetStorefront又名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 
28

我使用Page.Form.DefaultFocus和它的作品:

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

當沒有別的事情時,這對我有用,謝謝! – KenD 2013-02-07 14:12:58

+0

這應該是被接受的答案。 – Marcel 2017-11-10 15:29:06

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

} 
0

,當我提出登錄控制,自定義控制,並試圖在的OnInit()方法來查找UsernameTextBox我的問題arrized。 控件的OnInit在Page的OnInit之前執行,這就是爲什麼沒有創建Form控件的原因。

我將調用UsernameTextBox移動到OnLoad函數,它工作正常。

0

上述答案的工作對我來說,我只是嘗試:

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

...和它的工作!如果你不知道在`的FindControl()`不`cast`的成功

<asp:TextBox ID="TxtMyTextBoxName" type="search" runat="server"></asp:TextBox> 
相關問題