无法在我的Asp.Net页面中为TextBox填充AutoCompleteExtender的数据

时间:2013-07-30 13:27:22

标签: asp.net c#-4.0

我无法使用autocompleteextender将数据填充到文本框中,而是从数据库中获取数据。我在下面给出了我的代码。欢迎任何建议。 ASP.Net代码:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">
</asp:ToolkitScriptManager>
<div>       
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" UseContextKey="true" runat="server" CompletionInterval="10" TargetControlID="TextBox1" ServiceMethod="GetValues" MinimumPrefixLength="2" EnableCaching="false" CompletionSetCount="4">
    </asp:AutoCompleteExtender>
</div>

C#代码:

[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> GetValues(string prefixText,int count)
{
    List<string> lst = new List<string>();
    SqlConnection connection = new SqlConnection("Data Source=172.22.1.189;Initial Catalog=M1022779;Integrated Security=True");
    SqlCommand command = new SqlCommand();
    command.Connection = connection;
    command.CommandText = "select * from dept where dname like '%'+@Name+'%'";
    command.Parameters.AddWithValue("@Name", prefixText);
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataTable dt = new DataTable();
    da.Fill(dt);
    //Console.WriteLine(dt.Rows[0][0]);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        lst.Add(dt.Rows[i][1].ToString());
    }
    return lst;
}

1 个答案:

答案 0 :(得分:0)

在您的AutoCompleteExtender中:

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" UseContextKey="true" runat="server" CompletionInterval="10" TargetControlID="TextBox1" ServiceMethod="GetValues" MinimumPrefixLength="2" EnableCaching="false" CompletionSetCount="4">
    </asp:AutoCompleteExtender>

**UseContextKey="true"

您的方法应该有string contextKey

 [System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public static List<string> GetValues(string prefixText, int count, string contextKey)

或设置UseContextKey="false"