Example for output of this code:

User name

Step 1: Open visual studio start a new project.

Step 2:
Add a master page like below.

Add Master page

Step 3: Paste this code in Master page.

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head id="Head1" runat="server">
  4. <title></title>
  5. <asp:ContentPlaceHolder id="head" runat="server">
  6. <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" />
  7. <link href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css" />
  8. <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
  9. <script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  10. <script type="text/javascript">
  11. $(function ()
  12. {
  13. SearchText();
  14. });
  15. function SearchText()
  16. {
  17. $(".autosuggest")
  18. .autocomplete(
  19. {
  20. source: function (request, response)
  21. {
  22. $.ajax(
  23. {
  24. type: "POST",
  25. contentType: "application/json; charset=utf-8",
  26. url: "AutoCompleteService.asmx/GetAutoCompleteData",
  27. data: "{'name':'" + $('#Search')
  28. .val() + "'}",
  29. dataType: "json",
  30. success: function (data)
  31. {
  32. if(data.d.length > 0)
  33. {
  34. response($.map(data.d, function (item)
  35. {
  36. return {
  37. label: item.split('/')[0],
  38. val: item.split('/')[1]
  39. }
  40. }));
  41. }
  42. else
  43. {
  44. response([
  45. {
  46. label: 'No Records Found',
  47. val: -1
  48. }]);
  49. $('#Search')
  50. .val('');
  51. }
  52. },
  53. error: function (result)
  54. {
  55. alert("Error");
  56. }
  57. });
  58. },
  59. select: function (event, ui)
  60. {
  61. if(ui.item.val == -1)
  62. {
  63. return false;
  64. }
  65. //Get Selected Value
  66. //alert(ui.item.val);
  67. }
  68. });
  69. }
  70. </script>
  71. </asp:ContentPlaceHolder>
  72. </head>
  73. <body>
  74. <form id="form1" runat="server">
  75. <div class="demo">
  76. <div class="ui-widget">
  77. <label for="tbAuto"> UserName: </label>
  78. <input type="text" id="Search" class="autosuggest" /> </div>
  79. <div>
  80. <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder>
  81. </div>
  82. </form>
  83. </body>
  84. </html>
Step 4: Then add a websevice page. add --> New item-->web service.asmx and rename it as "AutoCompleteService.asmx"

Paste below code in AutoCompleteService.asmx page.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Services;
  7. namespace _9_nov_search_box_auto_suggetion
  8. {
  9. [System.Web.Script.Services.ScriptService]
  10. public class AutoCompleteService: System.Web.Services.WebService
  11. {
  12. [WebMethod]
  13. public List < string > GetAutoCompleteData(string name)
  14. {
  15. List < string > result = new List < string > ();
  16. using(SqlConnection con = new SqlConnection("Data Source=192.168.3.48;Initial Catalog=vikram;User ID=sa;Password=Atharvana01"))
  17. {
  18. using(SqlCommand cmd = new SqlCommand("select DISTINCT name from people1 where name LIKE '%'+@SearchText+'%'", con))
  19. {
  20. con.Open();
  21. cmd.Parameters.AddWithValue("@SearchText", name);
  22. SqlDataReader dr = cmd.ExecuteReader();
  23. while(dr.Read())
  24. {
  25. result.Add(dr["name"].ToString());
  26. }
  27. return result;
  28. }
  29. }
  30. }
  31. }
  32. }
Step 5: Add a new webform1.aspx.

Paste below code in webform1.aspx.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_9_nov_search_box_auto_suggetion.WebForm1" MasterPageFile="~/Site1.Master" %>
  2. <asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  3. <p>Child Page</p>
  4. </asp:Content>
Above web form code please change master page file address and change connection string , table name and all.