In Asp.Net AJAX we have one most use ful control AutoCompleteExtender which displays auto suggestions for user input.
Add AutoCompleteExtender control to our asp.net page as shown below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AutoCompleteExtender Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptmanager1" runat="server"></asp:ScriptManager>
<asp:TextBox ID="txtNames" runat="server" MaxLength="300"ToolTip="Enter Name"/>
<ajaxToolkit:AutoCompleteExtender ID="aceNames" runat="server"
TargetControl ID="txtNames" ServiceMethod="GetNames"
ServicePath="AutoComplete.asmx" MinimumPrefixLength="1"
CompletionInterval="1000" EnableCaching="true" CompletionSetCount="20"
DelimiterCharacters="," ShowOnlyCurrentWordInCompletionListItem="true"
FirstRowSelected="true">
</ajaxToolkit:AutoCompleteExtender>
</div>
</form>
</body>
</html>
As shown above, AutoCompleteExtender control needs web service to get the auto suggestions. Add web service to our project and name it as AutoComplete.asmx and add method GetNames() as shown below.
using System.ComponentModel;
using System.Web.Services;
using System.Linq;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
[ToolboxItem(false)]
publicclassAutoComplete : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod]
public string[] GetNames(string prefixText, int count)
{
string[] a1= newstring[4];
a1[0] = "ABCD";
a1[1] = "BCDE";
a1[2] = "CDEF";
a1[3] = "DEFG";
return a1.Where(a => a.ToString().ToLower().StartsWith(prefixText)).ToArray();
}
}
We have to provide web method for AutoCompleteExtender control to get the suggestions by passing the user input. Here web method is GetNames() which displays the names started with the user input character.