开发者

control.focus() after selectedIndexChanged

I need to focus on a textbox after an item has been selected from a dropdownlist.

I've tried control.focus() and setfocus().

The last thing I've tried was Set_Focus(dtbEffectiveDate.ClientID) inside the SelectedIndexChanged method with the folowing method.

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

    strScript = "<script language=javascript> window.setTimeout(""" + ControlName + ".focus();"",0); &开发者_开发问答lt;/script>"
    RegisterStartupScript("focus", strScript)
End Sub

I'm out of answers so any help would be awesome.


You should select the control in javascript with document.getElementById(id):

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

something like:

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

Edit: I'm not entirely sure of the correct VB-syntax for escaping the quotes around ControlName.


You need to use document.getElementById in javascript before you can call focus on it.

Try something like:

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


There is also a server-side Control.Focus method that will render the appropriate JavaScript automatically to put the focus on that control.

Edit

Here is an example:

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜