当前位置: 移动技术网 > IT编程>开发语言>.net > Ajax+asp.net智能匹配检索(含图含完整代码)

Ajax+asp.net智能匹配检索(含图含完整代码)

2017年12月12日  | 移动技术网IT编程  | 我要评论

濠江岁月下,时诗整容前,绝色倾国:落跑囚妃

如图:


本技术的核心是通过asp.net ajax control toolkit中的autocompleteextender控件实现。
autocompleteextender控件实现自动输入建议的功能,通过调用webservice或本页面对应的方法名来获取提示数据,供用户达到自动选择的功能。

实现过程:
1.首先建立数据大家随便啊,然后建立个简单的表。


2.新建1个ajax网站,名字自己随便起哈,在建一个主页面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.创建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;
}
}

完!
简单明了!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网