Ajax+asp.net智能匹配檢索(含圖含完整代碼)


本技術(shù)的核心是通過ASP.NET Ajax Control Toolkit中的AutoCompleteExtender控件實(shí)現(xiàn)。
AutoCompleteExtender控件實(shí)現(xiàn)自動(dòng)輸入建議的功能,通過調(diào)用WebService或本頁面對應(yīng)的方法名來獲取提示數(shù)據(jù),供用戶達(dá)到自動(dòng)選擇的功能。
實(shí)現(xiàn)過程:
1.首先建立數(shù)據(jù)大家隨便啊,然后建立個(gè)簡單的表。


2.新建1個(gè)Ajax網(wǎng)站,名字自己隨便起哈,在建一個(gè)主頁面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 的摘要說明
/// </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ì)的組件,請取消注釋以下行
//InitializeComponent();
}
//定義數(shù)組保存獲取的內(nèi)容
private string[] autoCompleteWordList = null;
//兩個(gè)參數(shù)“prefixText”表示用戶輸入的前綴,count表示返回的個(gè)數(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í)數(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();
}
//定位二叉樹搜索的起點(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++)
{ ///查看開頭字符串相同的項(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;
}
}
完!
簡單明了!
相關(guān)文章
ASP.net中實(shí)現(xiàn)基于UrlRewrite的防盜鏈功能
這篇文章主要介紹了ASP.net中如何實(shí)現(xiàn)基于UrlRewrite的防盜鏈,需要的朋友可以參考下2014-03-03
大型門戶網(wǎng)站實(shí)現(xiàn)的十四大技術(shù)小結(jié)
參考下大型門戶網(wǎng)站的技術(shù),大家可以盡量的備份好服務(wù)器。2010-10-10
IE10下Gridview后臺(tái)設(shè)置行高不起作用解決方法
GridView1.HeaderStyle.Height=17發(fā)現(xiàn)在IE10 中不起作用,經(jīng)過反復(fù)測試修改為e.Row.Cells[0].Height=17即可解決問題,有類似問題的朋友可以參考下哈2013-04-04
利用MS AJAX注冊Javascript命名空間并創(chuàng)建類
利用MS AJAX注冊Javascript命名空間并創(chuàng)建類...2007-10-10
手動(dòng)把a(bǔ)sp.net的類生成dll文件的方法
當(dāng)我們在開發(fā)的時(shí)候,有時(shí)會(huì)將一些方法封裝起來供別人調(diào)用,下面就是一種生成DLL的方法.2009-11-11
一步步打造簡單的MVC電商網(wǎng)站BooksStore(4)
這篇文章主要和大家一起一步步打造一個(gè)簡單的MVC電商網(wǎng)站,MVC電商網(wǎng)站BooksStore第四篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
淺談Asp.net Mvc之Action如何傳多個(gè)參數(shù)的方法
本篇文章主要介紹了Asp.net Mvc之Action如何傳多個(gè)參數(shù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
Asp.net Core中實(shí)現(xiàn)自定義身份認(rèn)證的示例代碼
這篇文章主要介紹了Asp.net Core中實(shí)現(xiàn)自定義身份認(rèn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

