Autocomplete Textbox In Asp.net
It is very cool to have Autocomplete Textbox In Asp.net for the textbox in the ASP.net. However if the Auto Complete is not responsive, then the users will feel that it is tedious to type the characters. As we need a very light weight and highly responsive solution, we have selected this WebService based approach rather than the Ajax Update Panel and Webpage to fetch the data.
Autocomplete Textbox In Asp.net with WebService
For fetching data the data is transferred from web server to client (JavaScript) using web service. So the data transfer between server and client is very minimal based on the configuration value MinimumPrefixLength. Thus you feel it is responsive. As some animations will give a better user expereience, it is purely optional and the AutoCompleteExtender allows that as well
This article needs Ajax Control kit. It needs to be downloaded separately.
Once downloaded, create a new website and select the Default.Aspx and then switch to design mode in the Web Form for designing the page.
In Web Form
While in design mode, open the tool box and right click and choose items. In the "Choose Toolbox Item" choose the downloaded AjaxControlToolKit.dll.
Now you will see a list Ajax controls in the tool Box.
Drag the AutoCompleteExtender to the Web form. This will register the Tool Kit in the top.
Right click the website and add a web service named AutoComplete.asmx
In Web Service
import AjaxControlToolkit using Imports AjaxControlToolkit in the top
Add a new function named as GetProducts with the following paramters
prefixText: Ajax control kit needs this string for getting the typed in characters from the screen
count: the number of rows to be returned
next step is to get data from DB
import System.Data using Imports System.Data in the top
import System.Data.SqlClient also
now get a datareader to fill up the Array list.
(I am not going to explain how to fetch the data from db as you can find the information in the asp.net data controls category)
I faced some issues while converting object array to string array, So checkout for the sample to get the clear picture.
Now its time to fill up the AutoCompleteExtender with the web service and web method we created just now.
Fill up the following
ServicePath =" AutoComplete.asmx"
ServiceMethod =" GetProducts "
MinimumPrefixLength ="1"
CompletionSetCount ="10"
If you are running the website now you may not get the suggestions when you type more than 1 Character(based on configuration MinimumPrefixLength ="1" ). Perhaps, The chances are the following line might be missed out
<System.Web.Script.Services.ScriptService ()>
It should be along with
<WebService(Namespace:="