How to autocomplete search in a textbox with one tap

<div style="position: absolute; top: 841px; left: 12%;">
    <asp:TextBox ID="txtHotel" runat="server" CssClass="search_hot_txtbox" ></asp:TextBox>   
</div>
<br>
<script type="text/javascript">
    $(function fnc() {
    $(".search_hot_txtbox").autocomplete({
            source: function(request, response) {
                $.ajax({
                url: "hotel-result.aspx/BindDatatoDropdown",
                    data: "{ 'cn': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return data; },
                    success: function(data) {
                        response($.map(data.d, function(item) {
                            return {
                                value: item.HotelName
                            }

                        }))
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        //                            alert(textStatus);
                    }
                });
            },
            minLength: 2
        });
    }); 
</script>




 protected void chk3_CheckedChanged(object sender, EventArgs e)
    {
        if (Session["List"] != null)
            UCPager1.GetItemsControl(1, 5, 0, chk3star.Checked, chk2star.Checked, chk1star.Checked, chk4star.Checked, chk5star.Checked, chkP1.Checked, chkP2.Checked, chkP3.Checked, chkP4.Checked, chkP5.Checked, txtHotel.Text, spP1.InnerText, spP2.InnerText, spP3.InnerText, spP4.InnerText, spP5.InnerText, new Repeater(), chkP6.Checked, chkP7.Checked, chkP8.Checked, spP6.InnerText, spP7.InnerText, spP8.InnerText);
        else
            UCPager1.GetItems();
    }

      

+3


source to share


1 answer


You need here to post a message, when you hit "enter" in the textbox, the browser does a postback, but if you want to do it with javascript, you need to trigger the button control.

So I put the button control and I can even hide it with css like:

<div style="position: absolute; top: 841px; left: 12%;">
    <asp:TextBox ID="txtHotel" runat="server" CssClass="search_hot_txtbox" onkeydown="return SendKeyEnterTo(event, 'btnGo');" />
    <asp:Button runat="server" ID="btnGo" Text="search" onclick="btnSearch_Click" style="display:none;" ClientIDMode="Static" />
</div>

      

and then using this simple javascript, I read the "enter" key from the textbox and activated this message after entering control.



function SendKeyEnterTo(e, IdToClick)
{
    // look for window.event in case event isn't passed in
    if (window.event) 
    { 
        e = window.event; 
    }

    if (e.keyCode == 13)
    {
        document.getElementById(IdToClick).click();
        return false;
    }
    else
    {
        return true;
    }
}

      

This one is onkeydown="return SendKeyEnterTo(event, 'btnGo');"

essential for reading the input of the textbox, and the button ClientIDMode="Static"

on the button is essential for keeping the same ID when it is rendered.

Also, please note that this code runs alongside autocomplete and I've tested and used it.

0


source







All Articles