国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:23:57
字體:
來源:轉載
供稿:網友
如圖:

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


本技術的核心是通過ASP.NET Ajax Control Toolkit中的AutoCompleteExtender控件實現。
AutoCompleteExtender控件實現自動輸入建議的功能,通過調用WebService或本頁面對應的方法名來獲取提示數據,供用戶達到自動選擇的功能。

實現過程:
1.首先建立數據大家隨便啊,然后建立個簡單的表。

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

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


2.新建1個Ajax網站,名字自己隨便起哈,在建一個主頁面Default.aspx.
3.在Default.aspx中添加1個ScriptManager控件、1個AutoCompleteExtender控件和1個TextBox控件,配置如下:

復制代碼 代碼如下:


<asp:ScriptManager runat="server" />
<cc1:AutoCompleteExtender runat="server" TargetControlID="TextBox1"
ServicePath="KeyFind.asmx" CompletionSetCount="10" MinimumPrefixLength="1" ServiceMethod="GetCompleteDepart">
</cc1:AutoCompleteExtender>
<asp:TextBox runat="server"></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;
// 如果數組為空
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;
}
}


完!
簡單明了!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高阳县| 湖南省| 福建省| 昌乐县| 增城市| 旬阳县| 三门峡市| 黄骅市| 内黄县| 襄汾县| 苍山县| 锡林浩特市| 孝昌县| 乌鲁木齐县| 米脂县| 盐亭县| 普宁市| 兴安县| 昆明市| 深圳市| 博爱县| 衢州市| 娄烦县| 永福县| 左贡县| 万年县| 东港市| 桐城市| 德昌县| 八宿县| 清水县| 阜新市| 遵化市| 吉首市| 阳春市| 龙里县| 太保市| 齐河县| 彝良县| 法库县| 平舆县|