Ajax intelligent match retrieval (including graphs and complete code)

Source: Internet
Author: User

All users who use Google search engine know that they can enter some keywords in the text box to display the list of related search prompts.

 

 

 

The core of this technology is implemented through the AutoCompleteExtender Control in ASP. NET Ajax Control Toolkit.

The AutoCompleteExtender control allows you to automatically enter the recommended function. You can call WebService or the method name on the current page to obtain the prompt data, so that you can automatically select the function.

 

Implementation process:

1. Create a simple table.

2. Create an Ajax website with its own name. Create a home page named Default. aspx.

3. Add one ScriptManager control, one AutoCompleteExtender control, and one TextBox Control to Default. aspx. The configuration is as follows:

        <asp:ScriptManager ID="ScriptManager1" runat="server" />        <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"                ServicePath="KeyFind.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">        </cc1:AutoCompleteExtender>        <asp:TextBox ID="TextBox1" runat="server" Width="352px" Height="27px"></asp:TextBox>

 

4. Create a Web service and name it KeyFind. asmx. This service provides intelligent retrieval.

5. Add the following code to the KeyFind. cs file of the KeyFind. asmx Web Service:

Using System; using System. web; using System. collections; using System. web. services; using System. web. services. protocols; // introduce the space using System. data; using System. data. oleDb; using System. configuration; // <summary> // summary of KeyFind /// </summary> [WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] // Add a service script (must be added; otherwise, the program cannot run normally) [System. web. script. services. scrip TService] public class KeyFind: System. web. services. webService {public KeyFind () {// If the designed component is used, uncomment the following line // InitializeComponent ();} // define the private string [] autoCompleteWordList = null; // The prefixText parameter indicates the prefix entered by the user, count indicates the number of returned [WebMethod] public String [] GetCompleteDepart (string prefixText, int count) {// check whether the parameter is null if (string. isNullOrEmpty (prefixText) = true | count <= 0) return null; // If The array is empty if (autoCompleteWordList = null) {// read the database content OleDbConnection conn = new OleDbConnection (@ "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Server. mapPath ("Ex18_02.mdb"); conn. open (); OleDbDataAdapter da = new OleDbDataAdapter ("select keyName from keyInfo where keyName like '" + prefixText + "% 'order by keyName", conn ); dataSet ds = new DataSet (); da. fill (ds); // read data from the content file to the temporary array string [] temp = New string [ds. tables [0]. rows. count]; int I = 0; foreach (DataRow dr in ds. tables [0]. rows) {temp [I] = dr ["keyName"]. toString (); I ++;} Array. sort (temp, new CaseInsensitiveComparer (); // assign the content of the temporary array to the returned array autoCompleteWordList = temp; if (conn. state = ConnectionState. open) conn. close () ;}// locate the start point of binary tree search int index = Array. binarySearch (autoCompleteWordList, prefixText, new CaseInsensitiveComparer (); I F (index <0) {// modify the start index = ~ Index;} // search for data that meets the condition int matchCount = 0; for (matchCount = 0; matchCount <count & matchCount + index <autoCompleteWordList. length; matchCount ++) {// view the items with the same start string if (autoCompleteWordList [index + matchCount]. startsWith (prefixText, StringComparison. currentCultureIgnoreCase) = false) {break ;}// process the search result string [] matchResultList = new string [matchCount]; if (matchCount> 0) {// copy the search result Array. copy (autoCompleteWordList, index, matchResultList, 0, matchCount);} return matchResultList ;}}

Complete!

Simple and clear!

 

 

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.