views:

464

answers:

3

Searchfield with autocomplete

I would like to make a searchfield with autocomplete, so when you write like 2 or 3 letters it will show a dropdownlist with results that match a column from a table in the database.

I have been reading all over the internet, and i have found out that it can be done with jQuery and AJAX Control Toolkit. But i don't know how to make it compare the search textbox and the database and show results that match?

UPDATE:

I have been trying to make it work with Autocomplete extender from Ajax control toolkit, but i don't know how to make the Webservice with SQL.

UPDATE2

Can't get the webservice to work, so have been looking at jQuery UI, which can do autocomplete but don't know how to get that working with the database either.. :p

UPDATE3

I'm still stuck on this, so if anyone who knows how to make a datasource(webservice) that works with either Autocomplete extender or jQuery UI, please help me.

I want the autocomplete to show: SELECT * FROM TABEL WHERE Title LIKE + '%' @Search + '%' And its an MSSQL Database

A: 

You can use Asp.net Ajax control toolkit.

http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AutoComplete/AutoComplete.aspx

The database can be searched as shown below.

select * from tbl_Country where countryName like '%xxx%'

You can call sql statement from a Service method. Service method can be a web service.

<ajaxToolkit:AutoCompleteExtender 
    runat="server" 
    ID="autoComplete1" 
    TargetControlID="myTextBox"
    ServiceMethod="GetCompletionList"
    ServicePath="AutoComplete.asmx"
</ajaxToolkit:AutoCompleteExtender>

Create an asmx web service in your web application named AutoComplete.asmx. Then create a web method GetCompletionList. Inside this method you can include System.Data.SqlClient and fire the query to the database.

Amitabh
If @Fogh is using jQuery already, why use MS Ajax toolkit?
Dustin Laine
Yes i know, but how do i get it to show results from the database?
Fogh
How do you use a SQL Statement together with an Autocomplete extender?
Fogh
How do i make the web service with SQL Statement then? :p
Fogh
I have been looking at several different tutorials on how to make the webservice for autocomplete but can't really get it work..
Fogh
A: 

Your best bet is to create a web service that you use to interact with. Here is a pretty detailed SO post on it.

http://stackoverflow.com/questions/240721/jquery-autocomplete-in-asp-net-webforms

Dustin Laine
Yes - or an HttpHandler :)
Adrian K
A: 

Watching this video and looking though the comments answered my question :)

http://www.asp.net/learn/AJAX-videos/video-122.aspx

Fogh