使用Ajax自动完成

I want to provide an autocomplete option for a text in my web applciation . I have the master data in the SQL server database table. I beowsed in google and found how to use autocomplte. all the examples use web service to do this. I cannot create a web service to impltement this. Is it possible to implement autocomplete by looking up values from database in code behind ? If so any one can provide any sample links for reference ?

Thanks in advance,

Jebli

It depends on the volume of data. There are 2 options:

  • send it to the client pre-emptively (perhaps as json or html) in the page source
  • let the client query it based on their input

The second is common if the data-volume is non-trivial, as you can query when (for example) they've entered 3 characters; very useful for names and other long lists.

Re the web-service; this doesn't have to be a full/complex web-service; just a simple route or ashx (for example) that returns the filtered data.

The jquery autocomplete plugin supports both scenarios, although this is now partly obsoleted by jquery ui plugin.

Is it possible to implement autocomplete by looking up values from database in code behind

Well, that is at the server - so you're essentially talking about the same "web service" that you say you can't do... I also think you should separate out the 2 functions (create page vs provide auto-complete results) into separate files (/pages/whatever).

A simple way would be to make a new aspx page that takes the autocomplete query as querystring parameters, looks up the result in a database and returns the response as XML og JSON.