0

试图让自动完成功能在文本框中工作,但似乎方法背后的代码永远不会触发。任何人都可以从下面看到问题吗?我尝试了各种示例/教程但没有效果。

<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();
}
4

1 回答 1

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>
于 2012-06-08T11:59:15.773 回答