简体   繁体   中英

How to postback without a button in an autocomplete textbox?

I bound the textbox to DB, but I don't know how to send the ID of the customer to show their data in another page when I click on a name from autocomplete textbox and the name is selected in textbox.

<script type="text/javascript">
    $(document).ready(function () {
        $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx'
        });
    });

</script>

Here is code behind

public class SearchHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context) 
    {
        string term = context.Request["term"] ?? "";
        List<string> listSearch = new List<string>();
        List<string> IDsearch = new List<string>();

        string cs = ConfigurationManager.AppSettings["dbpath"];
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("searchFriend", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter()
            {
                ParameterName="@term",
                Value = term
            });
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            while(dr.Read())
            {
                listSearch.Add(dr["UInfo"].ToString());
                IDsearch.Add(dr["UId"].ToString());
            }
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        context.Response.Write(js.Serialize(listSearch));  
    }
}

Use select event: http://api.jqueryui.com/autocomplete/#event-select

       $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx',
            select: function( event, ui ) {
               //submit form here
               document.forms[0].submit();
           }
        });

If you need to retrieve selected value, use close-event, then retrieve value from textbox and use it how you want:

       $('#txtSearch').autocomplete({
            source: 'SearchHandler.ashx',
            close: function( event, ui ) {

                 var userid = $(this).val();

                 //open new window with customer info
                 window.open('CustomerInfo.aspx?Customer=' + userid);


           }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM