通过文本框上的按键事件搜索数据

时间:2013-05-27 04:50:56

标签: asp.net vb.net search keypress

我的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>

如果我按下文本框,它应该自动按下搜索按钮,并重新绑定数据网格,但它没有那样做,是否有任何解决方案?

1 个答案:

答案 0 :(得分:1)

替换&amp;用+并试试。

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)")