<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <head> <title>AutoComplete Textbox with webservice using 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: "AutoComplete.asmx/GetAutoCompleteData", data: "{'username':'" + document.getElementById('contract_num').value + "'}", dataType: "json", success: function (data) { response(data.d); }, error: function (result) { alert("Error"); } }); } }); } </script> </head>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data; using System.Data.SqlClient; /// /// Summary description for AutoComplete /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class AutoComplete : System.Web.Services.WebService { public AutoComplete () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public List GetAutoCompleteData(string username) { List result = new List(); using (SqlConnection con = new SqlConnection("Data Source=192.168.100.252;Integrated Security=true;Initial Catalog=AMICASSA_Checklist")) { using (SqlCommand cmd = new SqlCommand("select DISTINCT Contract_num from rawdata where Contact_num LIKE '%'+@SearchText+'%'", con)) { con.Open(); cmd.Parameters.AddWithValue("@SearchText", username); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { result.Add(dr["Contract_num"].ToString()); } return result; } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)