<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>AutoComplete Box with jQuery</title> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script> <script type="text/javascript"> $(document).ready(function () { SearchText(); }); function SearchText() { $(".autosuggest").autocomplete({ source: function (request, response) { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Default.aspx/GetAutoCompleteData", data: "{'username':'" + document.getElementById('txtSearch').value + "'}", dataType: "json", success: function (data) { response(data.d); }, error: function (result) { alert("Error"); } }); } }); } </script> </head> <body> <form id="form1" runat="server"> <div class="demo"></div> <div class="ui-widget"> <label for="tbAuto">Enter UserName: </label> <input type="text" id="txtSearch" class="autosuggest" /> </div> </form> </body> </html> This is .cs file code. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Web.Services; public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public static string getconnectionstring() { return System.Configuration.ConfigurationManager.ConnectionStrings["crudconnection"].ConnectionString; } public static List<string> GetAutoCompleteData(string username) { List<string> result = new List<string>(); SqlConnection con = new SqlConnection(getconnectionstring(); using (SqlCommand cmd = new SqlCommand("select DISTINCT username from crudtable where username LIKE '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", username); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["username"].ToString()); } return result; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)