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"/>
<C0: AutoCompleteExtender ID = "AutoCompleteExtender1" runat = "server" TargetControlID = "TextBox1"
ServicePath = "KeyFind. asmx" CompletionSetCount = "10" MinimumPrefixLength = "1" ServiceMethod = "GetCompleteDepart">
</PC3: 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. ScriptService]
Public class KeyFind: System. Web. Services. WebService
{

Public KeyFind ()
{
// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
// Define an array to save the obtained content
Private string [] autoCompleteWordList = null;

// The Two Parameters "prefixText" indicate the prefix entered by the user, and count indicates the number of returned results.
[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, www.2cto.com
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 the data of the content file to a 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 ());
If (index <0)
{// Correction start point
Index = ~ Index;
}
// Search for qualified data
Int matchCount = 0;
For (matchCount = 0; matchCount <count & matchCount + index <autoCompleteWordList. Length; matchCount ++)
{// View items with the same start string
If (autoCompleteWordList [index + matchCount]. StartsWith (prefixText, StringComparison. CurrentCultureIgnoreCase) = false)
{
Break;
}
}
// Process search results
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!

 

 

From Ai Zhi Chen

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.