2011-03-07 46 views
0

從下拉列表中選擇一個項目後,我需要關注文本框。control.focus()selectedIndexChanged後

我試過control.focus()和setfocus()。

我試過的最後一件事是Set_Focus(dtbEffectiveDate.ClientID)與SelectedIndexChanged方法內的方法。

Protected Sub Set_Focus(ByVal ControlName As String) 
    Dim strScript As String 

    strScript = "<script language=javascript> window.setTimeout(""" + ControlName + ".focus();"",0); </script>" 
    RegisterStartupScript("focus", strScript) 
End Sub 

我沒有答案,所以任何幫助都會很棒。

回答

1

您應選擇具有document.getElementById(id)在JavaScript控制:

document.getElementById('"+ControlName+"').focus(); 

類似:

Protected Sub Set_Focus(ByVal ControlName As String) 
    Dim strScript As String 

    strScript = "<script language=javascript> window.setTimeout(document.getElementById('" + ControlName + "').focus();"",0); </script>" 
    RegisterStartupScript("focus", strScript) 
End Sub 

編輯:我不能完全肯定正確的VB語法的周邊逃避報價控件名稱。

+1

此外,你會想要做的'window.onload'確保對焦之前的控制也存在。 – mellamokb 2011-03-07 16:14:03

+0

「無法將焦點移到控件上,因爲它是不可見的,未啓用或不接受焦點的類型。」 – kyle 2011-03-07 16:31:45

+0

@kyle:你可以發佈一些代碼示例嗎?我們都提出的代碼應該可以工作,並且我們需要更多的細節來給出更好的答案。 – mellamokb 2011-03-07 16:47:29

1

您需要在javascript中使用document.getElementById,然後才能對其調用focus

嘗試類似:

elem = document.getElementById('theCorrectId'); 
elem.focus(); 
0

還有一個服務器端Control.Focus方法時將自動生成相應的JavaScript把重點放在該控件。

編輯

下面是一個例子:

Protected Sub MyDropDownList_SelectedIndexChanged(...) 
    MyTextBox.Focus() 
End Sub 
+0

我試過Control.Focus,沒有任何反應。我認爲沒有任何反應,因爲頁面沒有完全加載或者控件沒有完全暴露出發生重點 – kyle 2011-03-07 16:43:31

+0

@kyle:服務器端,而不是JavaScript。我用一個例子更新了帖子。如果沒有您的代碼示例,很難說出發生了什麼。 – mellamokb 2011-03-07 16:46:06

+0

我試過control.focus()和setfocus()。 < - 這兩個我用作服務器端方法。 – kyle 2011-03-07 16:50:33

0
Protected Sub ddlformtype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Set_Focus(dtbEffectiveDate.ClientID) 
End Sub 

Protected Sub Set_Focus(ByVal ControlName As String) 
    Dim strScript As String 

    strScript = "<script language=javascript> var x = document.getElementById('" + ControlName + "'); window.setTimeout(""x.focus()"",0); </script>" 
    RegisterStartupScript("focus", strScript) 
End Sub 
相關問題