• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1181
  • Last Modified:

jquery autocomplete or ASP AutoCompleteExtender

Hi experts,

I'm want to create an autocomplete textbox using either one of these 2 tools.

Using the ASP.NET AJAX Control Toolkit AutoCompleteExtender such as this one:

Using the jquery UI Autocomplete located here:

For discussion sake, I will talk about this example:

If you look at this example the autocomplete uses this SQL Server query:

select * from tblCustomer Where CompanyName like @myParameter

So when you type a letter or letters in the autocomplete textbox, it looks up similar spelled items from the CompanyName column  in the table called tblCustomer.

I already did this example and it worked great but my question is this.

Is it possible do something like the following:

Suppose in my tblCustomer table I had another column called CompanyID of integer datatype. So the table might look something like this:

CompanyID  CompanyName
10020            Acme Inc.
11234            Panda Express
14553            Hasbro inc.
14559            Pepsi Cola Company
14567            Mattell Inc.

Is it possible to set it up that in my autocomplete I can either type in a CompanyID or CompanyName when it does the autocomplete suggestion list.

So for example  if I typed "14" in the autocomplete textbox then these  items would show up, because it looked them up from CompanyID column:

14553            Hasbro inc.
14559            Pepsi Cola Company
14567            Mattell Inc.

Or if instead I typed "P" in the autocomplete textbox then these  items would show up, because it looked them up from CompanyName column:

11234            Panda Express
14559            Pepsi Cola Company

Do you know how I might modify the example mentioned above to do something like this?


Do you know a book or a website link tutorial to a similar example on the internet either using the jQuery UI Autocomplete or the ASP AJAX Toolkit AutoCompleteExtender and SQL Server?

To summarize, I want to set up autocomplete to allow the user to enter items from 2 different columns.

Thanks for any references or info you can give me.
2 Solutions
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I do this in classic asp with jquery and you should be able to convert.  The way this works is you have 2 pages.  page1 is where the autocomplete form is and page2 is a page to accept a get request and based on the get request, display data in json format.  As you type in page1, an ajax request goes to page2 that shows up as the autocomplete back on page1.

The get request is just one parameter.  You end up with page2 creating a recordset based on the querystring, ?term=sometthing.     If you have multiple columns to check, I create an array using a space as the delimiter.  If somebody types in the autocomplete box, "firstname lastname" or more likely, "fir las" it comes through as ?term=fir%20las.    In vbs I convert this to an array.    
arrTerm=split(term," ")

Open in new window

Now the trick is to build in the logic to determine how you want to determine if you are searching multiple columns or not.  This will depend on the columns you are searching.
Miguel OzSoftware EngineerCommented:
Basically you need to idetify whether prefixText  is an integer or alphanumeric string:

Your logic should look like: (It replaces this line:  cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%") in your codeproject link sample code)

if prefixText  is an integer then (For example you can use TryParse method)
    Add company Id parameter, for example your code should look like:  cmd.Parameters.AddWithValue("@CompanyID", prefixText + "%")
    Add company name parameter, for example your code should look like:  cmd.Parameters.AddWithValue("@CompanyName", prefixText + "%")
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now