0

我在我的 asp .net 页面后面有代码,可以在我的所有文本框中添加监听器属性,如下所示:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("UserID") Is Nothing Then
            Session("UserID") = Request.ServerVariables("LOGON_USER").Replace("ActiveDir\", "")
        End If
        Label7.Text = Session("UserID").ToString
        If Not IsPostBack Then
            If CAuth.AuthUser(Session("UserID"), "20") Then
                FillData()
                'txtInquiryDate.Text = DateTime.Now.ToString(txtInquiryDate_CalendarExtender.Format)
                txtAccount.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID & "',event)")
                txtAmount.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID & "',event)")
                txtCustName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID & "',event)")
                txtInquiryDate.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID & "',event)")

            Else
                Response.Redirect("NoAccess.aspx")
            End If
        End If

    End Sub

我的 ASPX 页面上有 javascript 函数。:

 <script type="text/javascript">
    function doClick(buttonName, e) {
        //the purpose of this function is to allow the enter key to 
        //point to the correct button to click.
        var key;

        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox

        if (key == 13) {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null) { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
    }
</script>

如果我按下文本框,它应该会自动按下搜索按钮,并重新绑定数据网格,但它没有这样做,有什么解决方案吗?

4

1 回答 1

1

用 + 替换 & 并尝试。

txtAccount.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)")
                    txtAmount.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)")
                    txtCustName.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)")
                    txtInquiryDate.Attributes.Add("onKeyPress", "doClick('" + btnSearch.ClientID + "',event)")
于 2013-05-27T05:15:00.150 回答