Auto complete textbox asp.net

{0 Comments}

In this post you will know how to add an Ajax auto complete feature to a textbox in asp.net using c#. Some point which you must remember before applying Ajax to your asp.net form.

  1.  There must a script manager on the page
  2.  Only one script manager is allowed per page.
  3.  Ajax toolkit dll file should must be there in bin folder of your project root folder

You can download code file for this link autocomplete, but remember please check for above settings and change its sql query in its method and finally connection string.

 So following are the steps which you can use to create Ajax auto complete extender enable textbox in asp.net.

  1.  First drag-drop a textbox or just double-click on the textbox option from the tool box.
  2. Then in designer mode there is a arrow hovering on text box click there and add auto complete extender or add following script to your asp.net form.

 HTML |  copy code |? 
1
<cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
2
            DelimiterCharacters="" Enabled="True" ServicePath="" TargetControlID="TextBox1">
3
        </cc1:AutoCompleteExtender>

And set target control id to your textbox id.

  • You can use design method to add a service method to page or to do manually add following property to the auto complete extender.

 C# |  copy code |? 
1
 ServiceMethod="GetCompletionList" 

 

  • On your c sharp code behind page add following method

 C# |  copy code |? 
1
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
2
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
3
    {
4
        return default(string[]);
5
    }

  • In this methode you have set what you want to get in auto complete menu. Following is the sample menu which you can use by just changing only the sql query according to your use, except the sql query every thing will help you.now your complete page methode is

 C# |  copy code |? 
01
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
02
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
03
    {
04
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conx"].ConnectionString);
05
        con.Open();
06
        DataSet ds = new DataSet();
07
        SqlDataAdapter ad = new SqlDataAdapter("select abc from xyz where pqr=’qwe’", con);
08
        ad.Fill(ds, "data");
09
        con.Close();
10
        string[] main = new string[0];
11
        for (int i = 0; i < ds.Tables[0].Rows.Count - 1; i++)
12
        {
13
            if (ds.Tables[0].Rows[i].ItemArray[0].ToString().ToLower().StartsWith(prefixText.ToLower()))
14
            {
15
                Array.Resize(ref main, main.Length + 1);
16
                main[main.Length - 1] = ds.Tables[0].Rows[i].ItemArray[0].ToString();
17
                if (main.Length == 20)
18
                    break;
19
            }
20
 
21
        }
22
        Array.Sort(main);
23
        return main;
24
 
25
    }

  •  Now the last thing you need to change is the minimum prefix length to 1 which is mostly 3 by default in the property of auto complete extender.

Leave a Comment

Your email address will not be published.