ajaxtoolkit AutoCompleteExtender无法正常工作

时间:2012-06-08 11:38:08

标签: c# asp.net autocomplete textbox

尝试获取自动完成功能以处理文本框但似乎背后的代码方法永远不会触发。谁能从下面看到这个问题?我尝试了各种样本/教程没有效果​​。

<asp:ScriptManager ID="SM1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel" runat="server">
        <ContentTemplate>
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
            <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtUserName" ServiceMethod="GetCompletionList"
            MinimumPrefixLength="2" CompletionInterval="10" EnableCaching="true" CompletionSetCount="3" UseContextKey="True">
            </ajaxToolkit:AutoCompleteExtender>
        </ContentTemplate>
    </asp:UpdatePanel>
代码背后的代码:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethod()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
   string connectionString = ConfigurationManager.ConnectionStrings["WMSDatabase"].ConnectionString;
   SqlConnection conn = new SqlConnection(connectionString);
   // Try to use parameterized inline query/sp to protect sql injection
   SqlCommand cmd = new SqlCommand("SELECT TOP " + count + " Alias FROM dbo.Users WHERE Alias LIKE '" + prefixText + "%'", conn);
   SqlDataReader oReader;
   conn.Open();
   List<string> CompletionSet = new List<string>();
   oReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
   while (oReader.Read())
   CompletionSet.Add(oReader["Alias"].ToString());
   return CompletionSet.ToArray();
}

1 个答案:

答案 0 :(得分:2)

尝试从代码中删除更新面板:

<asp:ScriptManager ID="SM1" EnablePageMethods="true" runat="server"></asp:ScriptManager>
            <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
            <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtUserName" ServiceMethod="GetCompletionList"
            MinimumPrefixLength="2" CompletionInterval="10" EnableCaching="true" CompletionSetCount="3" UseContextKey="True">
            </ajaxToolkit:AutoCompleteExtender>