ASP.NET - want to create autocomplete (search) text box in master page

Asked By Justin Deva on 19-Jun-12 12:32 AM


want to create autocomplete (search) text box in master that child pages can access the textbox....

Jitendra Faye replied to Justin Deva on 19-Jun-12 12:52 AM
Make use of Ajax AutoComplete Extender

<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">

The following method is used to populate the Auto complete list of customers


public static List<string> SearchCustomers(string prefixText, int count)

  using (SqlConnection conn = new SqlConnection())
    conn.ConnectionString = ConfigurationManager
    using (SqlCommand cmd = new SqlCommand())
    cmd.CommandText = "select ContactName from Customers where " +
    "ContactName like @SearchText + '%'";
    cmd.Parameters.AddWithValue("@SearchText", prefixText);
    cmd.Connection = conn;
    List<string> customers = new List<string>();
    using (SqlDataReader sdr = cmd.ExecuteReader())
      while (sdr.Read())
    return customers;
Note: It is very important that you keep the signature of the method same as what given above. The method must have two parameters namely
1. prefixText (string)
2. count (int)
Otherwise the method won’t work with AutoCompleteExtender.

Jitendra Faye replied to Justin Deva on 19-Jun-12 12:52 AM
If you want to use jquery and database then go for this link-

Here you will get complete steps.
dipa ahuja replied to Justin Deva on 19-Jun-12 03:27 AM
Step 1 : Take a TextBox , AutoCompleteExtender
<asp:TextBox runat="server" ID="txtInput" Width="300" />
<cc1:AutoCompleteExtender runat="server" ID="AutoComplete1"
Step 2 : Add ScriptManager on page with <Services> tag
<asp:ScriptManager ID="ScriptManager1" runat="server">
  <asp:ServiceReference Path="~/WebService.asmx" />
Step 3: Add a Webservice file in your website by Right click on website->Add new Item ->Web Service and add this code inside it..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
  public WebService()
  public string[] GetNames(string prefixText, int count)
    List<string> items = new List<string>(count);
    DataSet ds = new DataSet();
    string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(cs))
        string sql = "SELECT name FROM people WHERE name LIKE '" + prefixText + "%'";
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand(sql, connection);
    foreach (DataRow dr in ds.Tables[0].Rows)
    return items.ToArray();