不使用Web服务的ASP.Net AJAX控件工具包AutoCompleteExtender

时间:2014-03-27 17:57:22

标签: jquery asp.net ajax

我正在使用Asp.net 4.5并且我在不使用Web服务的情况下实现自动完成扩展程序,但它无法正常工作。这是我的代码

<div>
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                 <asp:AutoCompleteExtender ServiceMethod="SearchCustomers"
MinimumPrefixLength="21"
CompletionInterval="100" EnableCaching="true" CompletionSetCount="100"
TargetControlID="TextBox1"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">

     这是我的aspx代码: -

  [System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = "server=.; database=northwind; integrated security=true";
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select name from patientHistory where name like @SearchText +'%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["name"].ToString());
                }
            }
            conn.Close();
            return customers;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将EnablePageMethods添加到scriptmanager

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="True"></asp:ToolkitScriptManager>