2

I am trying to implement a search textbox with the Ajax Control Toolkit AutoCompleteExtender. The result should be a list of names matching the entered text however what gets displayed is the page source HTML, character by character, creating an extremely long list of single letters.

I have found and tried several samples but cannot get this to work. I am certain the database connection is valid and the SQL query when executed directly in MSSMS, returns the expected result. The AjaxControlToolkit is installed and works on other pages in the solution.

This issue was asked before ("Ajax Control Toolkit AutoCompleteExtender displays html source character by character of the current page as autocomplete suggestion list"). However for reasons of simplicity and maintainability I do not want to implement a WebService as this poster did.

acex.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>AutoCompleteExtender - Last Names</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TextBox ID="txbxLastName" runat="server"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
                TargetControlID="txbxLastName"
                MinimumPrefixLength="2" 
                EnableCaching="true" 
                CompletionSetCount="1" 
                CompletionInterval="1000" 
                ServiceMethod="GetLastNames">
            </asp:AutoCompleteExtender>
        </div>
    </form>
</body>
</html>

acex.aspx.cs

using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MCA
{
    public partial class acex : System.Web.UI.Page
    {
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetLastNames(string prefixText)
        {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
        SqlCommand cmd = new SqlCommand("SELECT [Last_Name] FROM [Entity_Person] WHERE [Last_Name] LIKE @Name+'%'", conn);
        cmd.Parameters.AddWithValue("@Name", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        conn.Open();
        da.Fill(dt);
        List<string> LastNames = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            LastNames.Add(dt.Rows[i][0].ToString());
        }
        return LastNames;
    }
}

}

Community
  • 1
  • 1
Writex
  • 43
  • 5

0 Answers0