Use the Asp.net Ajax-AutoComplete Control

Source: Internet
Author: User
Introduction:
The AutoComplete control is used when you enter the first few letters or Chinese characters in the text box. This control can prompt you all the data starting with these letters from the text or database that stores the data, for your convenience.

Important attributes
1. targetcontrolid: Specifies the control to implement the prompt function.
2. servicepath: the path of the WebService. The data extraction method is written in a WebService.
3. serveicemethod: name of the method used to extract data in WebService.
4. minimumprefixlength: used to set the number of letters entered by the user to display the prompt effect.
5. completionsetcount: set the number of rows of prompt data.
6. completioninterval: the time interval for obtaining books from the server, in milliseconds.

Example
Open vs2005 to create an ajaxcontroltoolkit website.
Upload the text file textfile.txt under the app_datafolder on the website and add data to it.

Add a Web Service under the root directory of the website named oec2003_autocomplete. The system automatically designs two parts: oec2003_autocomplete.asmx andCodeSome oec2003_autocomplete.cs files are automatically put into the app_code directory. Open the oec2003_autocomplete.cs file and add the getcompletelist method for getting data. The Code is as follows:

  1  Using  System; 2  Using  System. Web; 3  Using  System. collections; 4  Using  System. Web. Services; 5  Using  System. Web. Services. Protocols; 6  Using  System. IO;  7   8    9  /**/  ///     <Summary>    10  ///  AutoComplete Summary    11  ///     </Summary>   12  [WebService (namespace  =     "  Http://tempuri.org/  "  )]  13  [Webservicebinding (conformsto  =  Wsiprofiles. basicprofile1_1)]  14  [System. Web. Script. Services. scriptservice] 15  Public    Class  AutoComplete: system. Web. Services. WebService {  16    17    Public  AutoComplete (){  18    19    //  If you use the designed components, uncomment the following lines:    20   //  Initializecomponent ();    21  }  22    23  [Webmethod]  24    Public     String  Helloworld (){  25   Return     "  Hello World  "  ;  26  }  27    /**/  ///     <Summary>    28    /// Getcompletelist    29    ///     </Summary>    30    //  Define a static array to save the obtained data    31    Private     Static    String  [] Autocompletewordlist  =     Null  ;  32  [Webmethod]  33    Public  String [] getcompletelist (  String  Prefixtext,  Int  Count) 34  {  35    If  (Autocompletewordlist  =     Null  )  36  {  37    String  [] Temp  = File. readalllines (server. mappath (  "  ~ /App_data/textfile.txt  "  ));  38  Array. Sort (temp,  New  Caseinsensitivecomparer ());  39  Autocompletewordlist  =  Temp;  40  }  41   42    Int  Index  =  Array. binarysearch (autocompletewordlist, prefixtext,  New  Caseinsensitivecomparer ());  43    If  (Index  <     0  ) 44  {  45  Index  =     ~  Index;  46  }  47    48    Int  Matchingcount;  49   For  (Matchingcount  =     0  ; Matchingcount  <  Count  &&  Index  +  Matchingcount  <  Autocompletewordlist. length; matchingcount  ++  )  50 {  51    If  (  !  Autocompletewordlist [Index  +  Matchingcount]. startswith (prefixtext, stringcomparison. currentcultureignorecase ))  52  {  53    Break  ;  54 }  55  }  56  String [] returnvalue  =     New     String  [Matchingcount];  57    If  (Matchingcount  >    0  )  58  {  59  Array. Copy (autocompletewordlist, index, returnvalue,  0  , Matchingcount );  60  }  61    Return  Returnvalue;  62  }  63   64  } 

Because the file class is used in the above Code, you should add the following code:

 
  UsingSystem. IO;

Because you need to call the Web service on the client, you also need to add the following code
[System. Web. Script. Services. scriptservice]

Save the Web Service Code

Open the default. aspx generated by default in the root directory
Drag a Textbox Control and an autocompleteextender control to the page.
Set the attributes of the autocompleteextender control in the Properties window, as shown below:

  <  Ajaxtoolkit: autocompleteextender  ID  = "Autocompleteextender1"  Runat  = "Server"  Servicemethod  = "Getcompletelist"  Servicepath  = "Oec2003_autocomplete.asmx"  Enabled  = "True"  Minimumprefixlength  = "2" Completionsetcount  = "10"  Targetcontrolid  = "Textbox1"  >    </  Ajaxtoolkit: autocompleteextender  > 

The value of the count parameter in the Web Service is the value of the completionsetcount attribute.
Save the design page and set the default page as the start page. Press F5 and enter oe in the text box to view the desired result.

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.