欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2012年06月06日 22:36:10   作者:  
使用谷歌搜索引擎的用戶都知道,只要在文本框中輸入部分關(guān)鍵字,就能顯示相關(guān)搜索提示信息列表
如圖:


本技術(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控件,配置如下:

復(fù)制代碼 代碼如下:

<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文件下加入如下代碼:
復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論