txtAccountBarcode.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearchN.UniqueID + "').click();return false;}} else {return true}; ")
'To simplify the process to write javascript in vb code Public Sub CallJSFunction(ByVal strKey As String, ByVal strJScript As String) Me.Page.RegisterStartupScript(strKey, "<script language='javascript'>" + strJScript + "</script>") End Sub 'To Set the focus to the specific control Public Sub SetFocus(ByVal strControlID As String) CallJSFunction("setFocus", "document.getElementById('" + strControlID + "').focus()") 'CallJSFunction("setFocus", "document.getElementById('" + strControlID + "').select()") End Sub
注:后台执行JS似乎只有在页面提交或刷新时才有作用,直接起效只有使用前台JS实现。