实例说明 本实例为智能检索指定文件夹中的文件,当用户在文本框中输入“人事”关键字时,程序将智能检索以“人事”开头的文件,并且在文本框下方以列表的形式显示,供用户选择。如图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)新建一个AJAX网站,将其命名为Ex08_01,默认主页为Default.aspx。 (2)在Default.aspx页中添加一个ScriptManager控件、一个AutoCompleteExtender控件和一个TextBox控件,其中ScriptManager控件主要用于管理Web页面中的AJAX控件,AutoCompleteExtender控件实现自动完成功能,TextBox控件接收输入检索关键字。 (3)创建一个Web服务,将其命名为FindFile.asmx,该服务主要完成智能检索功能。 (4)在FindFile.asmx Web服务的FindFile.cs文件下实现代码如下: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.IO; //添加命名空间 /// <summary> /// FindFile 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] ///添加脚本服务(很重要) [System.Web.Script.Services.ScriptService()] public class FindFile : System.Web.Services.WebService { public static string[] autoCompleteFileList = null; public FindFile() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [System.Web.Services.WebMethod()] [System.Web.Script.Services.ScriptMethod()] public string[] GetFileList(string prefixText, int count) { ///检测参数是否为空 if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null; if (autoCompleteFileList == null) { ///获取FilePage文件夹下所有文件的名称 DirectoryInfo di = new DirectoryInfo(Server.MapPath("FilePage")); FileSystemInfo[] dis = di.GetFileSystemInfos(); if (dis == null || dis.Length <= 0 ) return null; ///将文件名称保存到临时数组中 string[] tempFileList = new string[dis.Length]; for (int i = 0; i < dis.Length; i++) { tempFileList[i] = dis[i].Name; } ///对数组进行排序 Array.Sort(tempFileList, new CaseInsensitiveComparer()); autoCompleteFileList = tempFileList; } ///定位二叉树搜索的起点 int index = Array.BinarySearch(autoCompleteFileList, prefixText, new CaseInsensitiveComparer()); if (index < 0) { ///修正起点 index = ~index; // ~ 运算符对操作数执行按位求补运算,其效果相当于反转每一位。 } ///搜索符合条件的文件名称 int matchCount = 0; for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteFileList.Length; matchCount++) { ///查看开头字符串相同的项 if (autoCompleteFileList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false) { break; } } ///处理搜索结果 string[] matchResultList = new string[matchCount]; if (matchCount > 0) { ///复制搜索结果 Array.Copy(autoCompleteFileList, index, matchResultList, 0, matchCount); } return matchResultList; } } (5)回到Default.aspx页的源视图,设置其AutoCompleteExtender控件属性值,代码如下: <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ServicePath="FindFile.asmx" ServiceMethod="GetFileList" MinimumPrefixLength="1" CompletionInterval="100" > </cc1:AutoCompleteExtender> |