Ajax智能匹配檢索(含圖含完整代碼)

來源:互聯網
上載者:User

 使用Google搜尋引擎的使用者都知道,只要在文字框中輸入部分關鍵字,就能顯示相關搜尋提示資訊列表。
 


 
 
本技術的核心是通過ASP.NET Ajax Control Toolkit中的AutoCompleteExtender控制項實現。
AutoCompleteExtender控制項實現自動輸入建議的功能,通過調用WebService或本頁面對應的方法名來擷取提示資料,供使用者達到自動選擇的功能。
 
實現過程:
1.首先建立資料大家隨便啊,然後建立個簡單的表。


2.建立1個Ajax網站,名字自己隨便起哈,在建一個首頁面Default.aspx.
 

3.在Default.aspx中添加1個ScriptManager控制項、1個AutoCompleteExtender控制項和1個TextBox控制項,配置如下:
        <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.建立1個Web服務,將其命名為KeyFind.asmx,該服務主要完成智能檢索功能。
5.在KeyFind.asmx Web服務的KeyFind.cs檔案下加入如下代碼:
 
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
//引入空間
using System.Data;
using System.Data.OleDb;
using System.Configuration;
/// <summary>
/// KeyFind 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//添加服務指令碼(必須添,否則程式不能正常運行)
[System.Web.Script.Services.ScriptService]
public class KeyFind : System.Web.Services.WebService
{

    public KeyFind()
    {
        //如果使用設計的組件,請取消注釋以下行
        //InitializeComponent();
    }
    //定義數組儲存擷取的內容
    private string[] autoCompleteWordList = null;

    //兩個參數“prefixText”表示使用者輸入的首碼,count表示返回的個數
    [WebMethod]
    public String[] GetCompleteDepart(string prefixText, int count)
    {
        ///檢測參數是否為空白
        if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;
        // 如果數組為空白 www.2cto.com
        if (autoCompleteWordList == null)
        {
            //讀取資料庫的內容
            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);
            //讀取內容檔案的資料到臨時數組
            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());
            //將臨時數組的內容賦給返回數組
            autoCompleteWordList = temp;
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
        //定位二叉樹搜尋的起點
        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
        if (index < 0)
        {   //修正起點
            index = ~index;
        }
        //搜尋合格資料
        int matchCount = 0;
        for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
        {   ///查看開頭字串相同的項
            if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
            {
                break;
            }
        }
        //處理搜尋結果
        string[] matchResultList = new string[matchCount];
        if (matchCount > 0)
        {   //複製搜尋結果
            Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
        }
        return matchResultList;
    }
}
 
完!
簡單明了!

 

 

摘自 愛智旮旯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.