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