How to Create jQuery AutoComplete Textbox in ASP.Net





There are so many ways to create an Auto Complete TextBox in ASP.NET like using an Ajax AutoComplete Control with Web Service and WCF. But here we will see how to use jQuery UI autocomplete functionality in ASP.NET.

Pre-requisites

  1. Create the simple table tblEmp with Id and EmpName Field in SQL Server
  2. Download the Plugin in the local system or use the online plugin as in the following:

    http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js
    http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js
    http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css"

You can see here what can you do from the Jquery-ui plugin.

http://jqueryui.com

Now we will see the AutoComplete functionality of the jQuery UI. In this sample I will use the online plugin.

Step 1: Create the aspx page with TextBox control and the jQuery required plugin in the Head portion of the HTML.

Step 2: Write the jQuery UI auto complete jQuery syntax with Ajax web method as given below.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxTest.WebForm1" %>  
  2.    
  3. <!DOCTYPE html>  
  4.    
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.      <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>  
  9.     <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>  
  10.     <link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />  
  11.    
  12.     <script type="text/javascript">  
  13.         $(function () {  
  14.             $("#txtEmpName").autocomplete({  
  15.                 source: function (request, response) {  
  16.                     var param = { empName: $('#txtEmpName').val() };  
  17.                     $.ajax({  
  18.                         url: "WebForm1.aspx/GetEmpNames",  
  19.                         data: JSON.stringify(param),  
  20.                         dataType: "json",  
  21.                         type: "POST",  
  22.                         contentType: "application/json; charset=utf-8",  
  23.                         dataFilter: function (data) { return data; },  
  24.                         success: function (data) {  
  25.                             response($.map(data.d, function (item) {  
  26.                                 return {  
  27.                                     value: item  
  28.                                 }  
  29.                             }))  
  30.                         },  
  31.                         error: function (XMLHttpRequest, textStatus, errorThrown) {  
  32.                             var err = eval("(" + XMLHttpRequest.responseText + ")");  
  33.                             alert(err.Message)  
  34.                            // console.log("Ajax Error!");  
  35.                         }  
  36.                     });  
  37.                 },  
  38.                 minLength: 2 //This is the Char length of inputTextBox  
  39.             });  
  40.         });  
  41.     </script>  
  42. </head>  
  43. <body>  
  44.     <form id="form1" runat="server">  
  45.     <div>  
  46.     EmpName : <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>  
  47.         <br />  
  48.         <br />  
  49.         <br />  
  50.     </div>  
  51.     </form>  
  52.      
  53. </body>  
  54. </html> 

Step 3: Write the code in the code behind file using Webmethod like this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.SqlClient;  
  4. using System.Web.Services;  
  5.    
  6. namespace AjaxTest  
  7. {  
  8.     public partial class WebForm1 : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.         }  
  13.    
  14.         [WebMethod]  
  15.         public static List<string> GetEmpNames(string empName)  
  16.         {  
  17.             List<string> Emp = new List<string>();  
  18.             string query = string.Format("SELECT EmpName FROM tblEmp WHERE EmpName LIKE '%{0}%'", empName);  
  19.             using (SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True"))  
  20.             {  
  21.                 using (SqlCommand cmd = new SqlCommand(query, con))  
  22.                 {  
  23.                     con.Open();  
  24.                     SqlDataReader reader = cmd.ExecuteReader();  
  25.                     while (reader.Read())  
  26.                     {  
  27.                         Emp.Add(reader.GetString(0));  
  28.                     }  
  29.                 }  
  30.             }  
  31.             return Emp;  
  32.         }  
  33.     }  

Summary

This article showed how to use a jQuery UI AutoComplete in ASP.NET using jQuery theme. Let me know if you have a better approach for this.