AutoComplete Not firing

前端 未结 2 812
灰色年华
灰色年华 2021-01-25 09:06

It all looks good below I have provided the 3 parts the service works fine if queried.

/// 
/// Summary description for AutoCompleteService
/// &l         


        
相关标签:
2条回答
  • 2021-01-25 09:47

    If the autocomplete is JQueryUI and you wish to provide some custom parameters to the autocomplete then you will need to provide a function to set the source. Have a look here this should help you to define it.

    Also as @Shadow-Wizzard said you may want to make sure that you have the right id's

    0 讨论(0)
  • 2021-01-25 09:50

    try the following code. that is working for me..

      $(document).ready(function () {
            $('[ID$=txtLastname]').live('keyup.autocomplete', function () {
    
                $(this).autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: '<%=ResolveUrl("~/Resources/WebService.asmx/LastName") %>',
                            data: "{ 'prefix': '" + request.term + "'}",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                response($.map(data.d, function (item) {
                                    return {
                                        label: item.split('-')[0],
                                        val: item.split('-')[1]
                                    }
                                }))
                            },
                            error: function (response) {
                                alert(response.responseText);
                            },
                            failure: function (response) {
                                alert(response.responseText);
                            }
                        });
                    },
                    select: function (e, i) {
                    },
                    minLength: 1
                });
            });
    

    Web method is

     [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string[] LastName(string prefix)
    {
        List<string> customers = new List<string>();
        using (SqlConnection conn = new SqlConnection())
        {
            string connectionstring = CCMMUtility.GetCacheForWholeApplication();
            conn.ConnectionString = connectionstring;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select distinct top(10) Lastname from tblusers where  " +
                "Lastname  like '%'+ @SearchText + '%' order by Lastname";
                cmd.Parameters.AddWithValue("@SearchText", prefix);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(string.Format("{0}", sdr["Lastname"]));
                    }
                }
                conn.Close();
            }
            return customers.ToArray();
        }
    }
    
    0 讨论(0)
提交回复
热议问题