The AutoComplete Extender
AutoComplete is an ASP.NET AJAX extender that can be attached to any TextBox control and will associate that control with a popup panel to display words that begin with the prefix typed into the TextBox.
Properties of AutoComplete extender
- CompletionInterval
- CompletionListCssClass
- CompletionListElementID
- CompletionListHighlightedItemCssClass
- CompletionListItemCssClass
- CompletionSetCount
- ContextKey
- DelimiterCharacters
- EnableCaching
- FirstRowSelected
- MinimumPrefixLength
- OnClientHidden
- OnClientHiding
- OnClientItemOut
- OnClientItemOver
- OnClientItemSelected
- OnClientPopulated
- OnClientPopulating
- OnClientShowing
- OnClientShown
- OnHide
- OnShow
- ServiceMethod
- ServicePath
- ShowOnlyCurrentWordInCompletionListItem
- UseContextKey
Let us demonstrate the preceding explanation in a practical approach by creating a sample web application.
- CREATE TABLE [dbo].[Employee](
- [id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](50) NULL,
- [City] [varchar](50) NULL,
- [Address] [varchar](50) NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
- Provide the web site a name such as "CommaSepratedAutocomplete" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - Add Web Form.
- Drag and drop one textBoxes onto the <form> section of the Default.aspx page.
- Add AutoCompleteExtender from ToolBox.
- Now the default.aspx page source code will look such as follows.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Article by Vithal Wadje</title>
- </head>
- <body bgcolor="silver">
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
- </asp:ScriptManager>
- <br /><br />
- <table width="100%">
- <tr>
- <td>
- Name
- </td>
- <td>
- <asp:TextBox ID="TextBox1" runat="server" Height="47px" Width="317px"></asp:TextBox>
- <asp:AutoCompleteExtender
- ServiceMethod="GetCompletionList"
- MinimumPrefixLength="1"
- CompletionInterval="10"
- EnableCaching="false"
- CompletionSetCount="1"
- TargetControlID="TextBox1"
- ID="AutoCompleteExtender1"
- runat="server"
- FirstRowSelected="false"
- DelimiterCharacters=","
- ShowOnlyCurrentWordInCompletionListItem="true"
- >
- </asp:AutoCompleteExtender>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- [System.Web.Script.Services.ScriptMethod()]
- [System.Web.Services.WebMethod]
- public static List<string> GetCompletionList(string prefixText, int count)
- {
- using (SqlConnection con = new SqlConnection())
- {
- con.ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
- using (SqlCommand com = new SqlCommand())
- {
- com.CommandText = "select Name from Employee where " + "Name like @Search + '%'";
- com.Parameters.AddWithValue("@Search", prefixText);
- com.Connection = con;
- con.Open();
- List<string> countryNames = new List<string>();
- using (SqlDataReader sdr = com.ExecuteReader())
- {
- while (sdr.Read())
- {
- countryNames.Add(sdr["Name"].ToString());
- }
- }
- con.Close();
- return countryNames;
- }
- }
- }
- ShowOnlyCurrentWordInCompletionListItem="true"
- DelimiterCharacters=";"
- DelimiterCharacters=";"
- For the detailed code, please download the sample Zip file.
- Perform changes in the Web.config file, depending on your server location.
- You need to use the Ajax Control Toolkit library.
- To download the latest Ajax control toolkit, click here Ajax ContolToolkit Download .

Vithal WadjePosted Mar 22, 2015, 12:01 AM
Thanks Harpreet Singh sir,yes i am currently preparing on it,soon i will upload
Harpreet SinghPosted Mar 21, 2015, 3:12 PM
Very useful...sir I am waiting for part 2 of Membership API..please upload it soon.. Thank you
Vithal WadjePosted Mar 21, 2015, 12:06 PM
Thanks Gowtham Rajamanickam sir
Gowtham RajamanickamPosted Mar 21, 2015, 11:48 AM
informative..
Pankaj Kumar ChoudharyPosted Mar 21, 2015, 11:44 AM
Nice article sir but there is a little mistake. In hypan (-) Separated search your write DelimiterCharacters=";" but right option is DelimiterCharacters="-"