Sabilv
Sabilv

Reputation: 600

Search the data by keypress event on the textbox

i have code behind from my asp .net page, to add listener atribute on all my text boxes like this :

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

and there's javascript function on my ASPX page. :

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

if i keypress the text boxes, it should automaticly like press the search button, and rebind the datagrid, but it didn't do that, is there any solutions?

Upvotes: 0

Views: 3022

Answers (1)

Anuj
Anuj

Reputation: 1526

replace & with + and try.

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

Upvotes: 1

Related Questions