Using Ajax Auto-complete extender control

Now its easy to use auto-complete or auto-suggest text box in your asp.net project or website. It’s quite simple. Follow the steps carefully.

Step 1: Open visual studio

Step 2: Select a ajax enabled webapplication or ajax enabled website

Step 3: In the design view of default.aspx page, make sure script manager is in the page and if not add a script manager from the ajax tool controls

Step 4: Drag and drop the textbox and autocomplete extender control and rename textbox as mytextbox.

Step 5: Copy the access database you want to use to app_data folder of the website

Step 6: Right click on the app_data folder and then add existing item and add the mdb file and you can see the database appear in the application folder.Right click on the project->Add new item ->add a webservice and rename it as autocomplete.asmx.
( In case this is the first time you are creating a webservice you may get a prompt asking if you need to put the code in a separate class file. Select Yes )

Step 7: Now paste the code in autocomplete.cs file under the app_code folder. Change the query string and other credentials as applicable to your project

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : WebService
{
[WebMethod]
public string[] GetCompletionList(string prefixText,int count, string contextKey)
{
count = 10;
string querystring;
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/user/My Documents/Visual Studio 2008/Projects/WebApplication1/WebApplication1/App_Data/data.mdb");
querystring = "select distinct student_name from student_query where course_name like @prefixText";
OleDbDataAdapter da = new OleDbDataAdapter(querystring, aConnection);
da.SelectCommand.Parameters.Add("@prefixtext", OleDbType.VarChar, 50).Value = prefixText + "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr[0].ToString(), i);
i++;
}
return items;
}
}

Step 8: In the default.aspx page make the following changes
For the autocomplete extender include the following properties

TargetControlID="myTextBox" ServicePath="AutoComplete.asmx" ServiceMethod="getCompletionlist" MinimumPrefixLength="1" UseContextKey="true" ContextKey="course">

Step 9 : Now if you run the project you should be able to see the suggestions for a particular letter depending on the words in your database.

Tips: If you have done all the above steps are still you are not able to find what is wrong with your project , there should be something wrong with your config file.
You don’t have to give up. Follow these steps now,
Go to this page
Watch the video if you are new and then download the c# code from there. Make necessary modification according to your project as you already have the webservice code to connect to access database. With little modification of the query strings you can use SQL datasource too.

Comments are welcome. Please let me know if this works for you.

7 Responses to “Using Ajax Auto-complete extender control”


  1. 1 Eraslan August 24, 2008 at 7:55 pm

    UseContextKey=”true” ContextKey=”course” when i write them i see an error

    Validation (ASP.Net): Attribute ‘UseContextKey’ is not a valid attribute of element ‘AutoCompleteExtender’

    do you have an idea what can i do

  2. 2 tools August 26, 2008 at 8:55 am

    hi! i read your codes. Have you the project for me download? Thanks

  3. 3 funwithtechnology September 9, 2008 at 4:46 pm

    In this case the “Contextkey = course” is the name of the table in the database i’m using. Change this according to the name of the table your are using. Hope this helps.

  4. 4 funwithtechnology September 9, 2008 at 4:48 pm

    @tools

    you can download the C# code below the video link I mentioned above and change the code according to your needs.

  5. 5 Gokhan February 21, 2009 at 7:59 pm

    Many Thanks

  6. 6 bastian February 23, 2009 at 4:52 pm

    hi
    i’m having the same problem
    Validation (ASP.Net): Attribute ‘UseContextKey’ is not a valid attribute of element ‘AutoCompleteExtender’
    i have db named db1 placed in App_Data folder
    inside of it I create table course
    but the error still the same


  1. 1 Bookmarks about Control Trackback on December 10, 2008 at 10:30 pm

Leave a Reply