Ref:ASP.Net AJAX Control Toolkit AutoCompleteExtender without using Web Services
HTML:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server" AutoPostBack="true" OnTextChanged="GetContactDetails"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtContactsSearch"
ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false">
</cc1:AutoCompleteExtender>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
C#:
protected void GetContactDetails(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from Customers where " +
"Country = @SearchText ";
cmd.Parameters.AddWithValue("@SearchText", this.txtContactsSearch.Text.Trim());
cmd.Connection = conn;
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
conn.Close();
}
}
}
[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 = ConfigurationManager
.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select DISTINCT(Country) from Customers where " +
"Country 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["Country"].ToString());
}
}
conn.Close();
return customers;
}
}
}
Output:
Thank You.