Ajax+asp.net智能匹配檢索(含圖含完整代碼)
本技術(shù)的核心是通過ASP.NET Ajax Control Toolkit中的AutoCompleteExtender控件實現(xiàn)。
AutoCompleteExtender控件實現(xiàn)自動輸入建議的功能,通過調(diào)用WebService或本頁面對應(yīng)的方法名來獲取提示數(shù)據(jù),供用戶達(dá)到自動選擇的功能。
實現(xiàn)過程:
1.首先建立數(shù)據(jù)大家隨便啊,然后建立個簡單的表。
2.新建1個Ajax網(wǎng)站,名字自己隨便起哈,在建一個主頁面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.創(chuàng)建1個Web服務(wù),將其命名為KeyFind.asmx,該服務(wù)主要完成智能檢索功能。
5.在KeyFind.asmx Web服務(wù)的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)]
//添加服務(wù)腳本(必須添,否則程序不能正常運行)
[System.Web.Script.Services.ScriptService]
public class KeyFind : System.Web.Services.WebService
{
public KeyFind()
{
//如果使用設(shè)計的組件,請取消注釋以下行
//InitializeComponent();
}
//定義數(shù)組保存獲取的內(nèi)容
private string[] autoCompleteWordList = null;
//兩個參數(shù)“prefixText”表示用戶輸入的前綴,count表示返回的個數(shù)
[WebMethod]
public String[] GetCompleteDepart(string prefixText, int count)
{
///檢測參數(shù)是否為空
if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;
// 如果數(shù)組為空
if (autoCompleteWordList == null)
{
//讀取數(shù)據(jù)庫的內(nèi)容
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);
//讀取內(nèi)容文件的數(shù)據(jù)到臨時數(shù)組
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());
//將臨時數(shù)組的內(nèi)容賦給返回數(shù)組
autoCompleteWordList = temp;
if (conn.State == ConnectionState.Open)
conn.Close();
}
//定位二叉樹搜索的起點
int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
if (index < 0)
{ //修正起點
index = ~index;
}
//搜索符合條件的數(shù)據(jù)
int matchCount = 0;
for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
{ ///查看開頭字符串相同的項
if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
{
break;
}
}
//處理搜索結(jié)果
string[] matchResultList = new string[matchCount];
if (matchCount > 0)
{ //復(fù)制搜索結(jié)果
Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);
}
return matchResultList;
}
}
完!
簡單明了!
相關(guān)文章
ASP.NET MVC SSO單點登錄設(shè)計與實現(xiàn)代碼
本篇文章主要介紹了ASP.NET MVC SSO單點登錄設(shè)計與實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。2017-01-01如何在ASP.Net Core中使用 IHostedService的方法
這篇文章主要介紹了如何在ASP.Net Core中使用 IHostedService的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02ASP.Net PlaceHolder、Panel等控件未實現(xiàn)INamingContainer,導(dǎo)致FindContro
這2天在開發(fā)中發(fā)現(xiàn),如果在new的Panel中使用FindControl,會出現(xiàn)找不到控件的情況2009-06-06ASP.NET中Webservice安全 實現(xiàn)訪問權(quán)限控制
本文主要講解ASP.NET中的Webservice的安全設(shè)置兩種方法,一種基于soapheader,一種基于SoapExtensionAttribute,需要的朋友可以參考下。2016-05-05ASP.NET2.0 SQL Server數(shù)據(jù)庫連接詳解
本文將詳細(xì)介紹如何使用Connection對象連接數(shù)據(jù)庫 。對于不同的.NET 數(shù)據(jù)提供者,ADO.NET采用不同的Connection對象連接數(shù)據(jù)庫。這些Connection對象為我們屏蔽了具體的實現(xiàn)細(xì)節(jié),并提供了一種統(tǒng)一的實現(xiàn)方法。2009-07-07asp.net sql 數(shù)據(jù)庫處理函數(shù)命令
asp.net sql 數(shù)據(jù)庫處理函數(shù)命令 ,需要的朋友可以參考下。2009-10-10