ASP.NET+ajax智能文件检索(自动完成)
2015-03-18 13:52
239 查看
实例说明
本实例为智能检索指定文件夹中的文件,当用户在文本框中输入“人事”关键字时,程序将智能检索以“人事”开头的文件,并且在文本框下方以列表的形式显示,供用户选择。如图18.1所示。图18.1 智能文件检索
技术要点
本实例的核心技术是通过ASP.NET AJAX Control Toolkit中的AutoCompleteExtender控件实现。AutoCompleteExtender控件实现自动输入建议的功能,通过调用WebService或本页面对应的方法名来获取提示数据,供用户能达到自动选择的功能。AutoCompleteExtender控件的主要属性如表18.1所示。
表18.1 AutoCompleteExtender控件的主要属性
属性 | 说明 |
TargetControlID | 指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox |
ServicePath | 指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名 |
ServiceMethod | 指出提供服务的方法名,例如public string[] Method(string prefixText, int count),其中参数prefixText是用户输入的关键字;参数count是所需要获取提示数据的数量;两个参数都会自动传给WebService的ServiceMethod方法),返回值是用户所获得提示数据的来源数组。 |
MinimumPrefixLength | 指出开始提供提示服务时,TextBox控件应有的最小字符数,默认值为3 |
CompletionInterval | 从服务器读取数据的时间间隔,默认为1000,单位:毫秒。 |
EnableCaching | 是否在客户端缓存数据,默认为true |
CompletionSetCount | 显示的条数,默认值为10 |
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;
}
}
相关文章推荐
- ASP.net+AJAX智能匹配检索(自动完成)
- ASP.net+AJAX智能匹配检索(自动完成)
- ASP.NET+ajax智能文件检索(自动完成)
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- EditPlus的编码自动完成功能及配置文件下载
- ccnet +msbuild +svn 每日构建(二 )编译完成后自动发布并部署文件
- Matlab自动完成文件for Notepad++
- 使用FTP命令自动完成文件的上传和下载
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 自动生成控制文件及数据文件并完成sqlldr的自动导入
- C++读写文件代码自动生成器(python3完成)
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 使用EditPlus技巧,提高工作效率(自动完成文件、语法文件下载)
- ASP.net+AJAX智能匹配检索(自动完成)
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- EditPlus的编码自动完成功能及配置文件下载
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- 如何在vs的setup project中,设置安装完成后自动打开一个html文件?
- 使用EditPlus技巧,提高工作效率(附英文版、自动完成文件、语法文件下载)
- android 应用升级 下载安装包 完成后 自动安装apk文件