Introduction

This article explains how to implement an auto-complete text box in ASP.NET using jQuery.
The following is my Data Table from which I am showing Employee Name:
design view
Image 1
The records in my Data Table are shown below.
Here I will show Employee Name in the autocomplete text box.
employee name
Image 2
Now my aspx code is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>AutoComplete Text Box using jQuery in ASP.NET</title>
  6. <link href="jquery-ui.css" rel="stylesheet" type="text/css" />
  7. <script src="jquery.min.js" type="text/javascript"></script>
  8. <script src="jquery-ui.min.js" type="text/javascript"></script>
  9. <script type="text/javascript">
  10. $(document).ready(function() {
  11. SearchText();
  12. });
  13. function SearchText() {
  14. $("#txtEmpName").autocomplete({
  15. source: function(request, response) {
  16. $.ajax({
  17. type: "POST",
  18. contentType: "application/json; charset=utf-8",
  19. url: "Default.aspx/GetEmployeeName",
  20. data: "{'empName':'" + document.getElementById('txtEmpName').value + "'}",
  21. dataType: "json",
  22. success: function(data) {
  23. response(data.d);
  24. },
  25. error: function(result) {
  26. alert("No Match");
  27. }
  28. });
  29. }
  30. });
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <form id="form1" runat="server">
  36. <table cellpadding="10" cellspacing="10" style="border: solid 15px Green; background-color: SkyBlue;"
  37. width="50%" align="center">
  38. <tr>
  39. <td>
  40. <span style="color: Red; font-weight: bold; font-size: 18pt;">Enter Employee Name:</span>
  41. <asp:TextBox ID="txtEmpName" runat="server" Width="160px" />
  42. </td>
  43. </tr>
  44. </table>
  45. </form>
  46. </body>
  47. </html>
Now my aspx.cs code is:
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Web.Services;
  13. using System.Data.SqlClient;
  14. using System.Collections.Generic;
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. }
  20. [WebMethod]
  21. public static List<string> GetEmployeeName(string empName)
  22. {
  23. List<string> empResult = new List<string>();
  24. using (SqlConnection con = new SqlConnection(@"Data Source=SARSHA\SqlServer2k8;Integrated Security=true;Initial Catalog=Test"))
  25. {
  26. using (SqlCommand cmd = new SqlCommand())
  27. {
  28. cmd.CommandText = "select Top 10 EmployeeName from Employee where EmployeeName LIKE ''+@SearchEmpName+'%'";
  29. cmd.Connection = con;
  30. con.Open();
  31. cmd.Parameters.AddWithValue("@SearchEmpName", empName);
  32. SqlDataReader dr = cmd.ExecuteReader();
  33. while (dr.Read())
  34. {
  35. empResult.Add(dr["EmployeeName"].ToString());
  36. }
  37. con.Close();
  38. return empResult;
  39. }
  40. }
  41. }
  42. }
Now Run the Application
Here type some letter and see the Employee Name List:
enter employee name
Image 3
output
Image 4