This article mainly introduces the asp.net + ajax + SQL Server automatic complement function, the need for friends can refer to the following
Code download Description: Database connection string in Web.config file, the official Northwind database is used for easy running. references (to its authors): ²http://www.loveweb8.com/plus/demo.php?aid=57 This example is the HTML source code. Using JS Jquery.autocomplete plug-in to achieve the automatic completion of the full function. Because my need is to combine SQL Server database table to realize automatic complement function. Down is the database table into JS array, naturally think of Ajax. Two basic development models of &NBSP;²ASP.NETAJAX the second part of this article is that JS calls the example of WebService. Code resolution. 1. Add WebService file. Add New Item--AJAX-enabled WCF service names the new file dbservice.svc. 2. Add a function to the dbservice.svc, and the function return value is the data that prompts for automatic completion. Code as follows: [operationcontract] public string getsortlist () { list<string> sorts = new List<s Tring> (); using (SqlConnection cn = new SqlConnection (configurationmanager.connectionstrings["zhui.pc"). ConnectionString)) { CN. Open (); SqlCommand cmd = new SqlCommand ("SELECT [LastName] FROM [dbo].[ Employees] ", CN); datatable dt = new DataTable (); using (SqlDataAdapter da = new SqlDataAdapter (cmd)) {& nbsp Da. Fill (DT); } &nbsP foreach (DataRow row in dt. Rows) { sorts. ADD (Row[0]. ToString ()); } CN. Close (); the} return string. Join (",", sorts.) ToArray ()); } 3. In default.aspx file add js, css file: code as follows: <script src= "scripts/ Jquery-1.7.1.min.js "type=" Text/javascript "></script> <script src=" scripts/ Jquery.autocomplete.min.js "type=" Text/javascript "></script> <link href=" styles/ Jquery.autocomplete.css "rel=" stylesheet "type=" Text/css "/> 4. In the head section of the Default.aspx $ (). Ready ( function ()), call WebService to obtain the automatically-filled data, and associate the data with the input box. Code as follows: <script type= "Text/javascript" > $ (). Ready (function () { Newssort.getsortlist ( OnComplete, onfailed, null); function OnComplete (args, context) { $ (' #MainContent_searchBox '). AutoComplete ({ ' data ': Args.split (","), ' itemheight ': 20, ' listdirection ': ' Down ', ' width ': 280 }). AutoComplete (' show '); } &NBsp function onfailed (args) { alert ("Error!") "); } }); </script> 5.Finish.