Analysis of Asp.net+ajax+sqlserver automatic complement function realization

Source: Internet
Author: User
Tags min sorts

  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    ²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.  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.