AjaxToolkit AutoComplete Extender In ASP.NET

Sometimes there is a need for some suggested text when searching for specific text from a huge number of records and search multiple records. This type of requirement can be satisfied using the Ajax Control toolkit Auto Complete extender. So let us implement this requirement step-by-step.  
What is 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.
Step 1: Create a table using the following script:
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]  
Insert some records into a table such as follows:
 
Step 2: Create a web application as in the following:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "CommaSepratedAutocomplete" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - Add Web Form.
  5. Drag and drop one  textBoxes onto the <form> section of the Default.aspx page.
  6. Add AutoCompleteExtender from ToolBox.
  7. 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> 
Step 4: Write a method in the default.aspx.cs page to bind records from the database as in the following:
    [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;  
                   
                    
               }  
               
           }  
          
       } 
Comma Separated search
To allow a comma-separated search set the ajax AutoComplete extender property as DelimiterCharacters="," . Now run the application and the UI will look as follows:
 
Now type something into the TextBox and select the first text and provide a comma and again type into the TextBox, it will show search text as in the following:
 
Now you have seen how comma-separated text appears, the first selected text appears again. To avoid this, set the autocomplete extender property as in the following:
ShowOnlyCurrentWordInCompletionListItem="true"
 Now type after the comma, then the text will appears are as follows:
 
Now the only current text is appearing after the search.
Semi-colon separated search
You can also use a semicolon to search. For that we need to set the  autocomplete extender DelimiterCharacters property as in the following:
DelimiterCharacters=";" 
Now run the application and type after the semicolon. Then the text will appear as follows:
 
Hyphen (-)  Separated search
You can also use a hyphen to search, for that we need to set the autocomplete extender DelimiterCharacters property as in the following:
DelimiterCharacters=";"   
Now run the application and type after the hyphen then the text will appear as follows:
 
Similar to this, you can use any delimiter character to separate the search.
From all the preceding examples, we have learned how to search using special characters to separat the text .
Notes
  • 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 .
Summary
For all the examples above, we have learned how to use the Ajax auto-complete extender to search using special charters. I hope this article is useful for all readers. If you have a suggestion then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode