让我们通过一个实例来测试上述两种方法:
首先,让我们建立一个词库,提供自动完成的列表。这个词库Copy自Atlas的官方文档示例,是一些.NET的常见术语。将其存为WordData.txt并置于App_Data目录下。
Word List access control list (ACL)ADO.NETaggregate eventalpha channelanchoringantialiasingapplication baseapplication domain (AppDomain)application manifestapplication stateASP.NETASP.NET application services databaseASP.NET mobile controlsASP.NET mobile Web FormsASP.NET pageASP.NET server controlASP.NET Web applicationassemblyassembly cacheassembly manifestassembly metadataassertion (Assert)association classASSOCIATORS OFasynchronous methodattributeauthenticationauthorizationautopostbackboundsboxingC#cardcatalogCCWchevronchromecHTMLCIMCIM Object ManagerCIM schemaclassclient areaclient coordinatesclipclosed generic typeCLRCLSCLS-compliantcode access securitycode-behind classcode-behind filecode-behind pageCOM callable wrapper (CCW)COM interopCommon Information Model (CIM)common language runtimecommon language runtime hostCommon Language Specification (CLS)common object file format (COFF)common type system (CTS)comparison evaluatorcomposite controlconfiguration fileconnectionconnection pointconstraintconstructed generic typeconstructed typeconsumercontainercontainer controlcontent pagecontextcontext propertycontractcontrol statecross-page postingCTScustom attribute (Attribute)custom control
然后创建一个Web Service用来提供建议列表,其中逻辑不多讲了,大概是读入上面的词库并根据输入找出相关的词汇,注意一下GetWordList方法的签名。
Suggestion Web Service Code using System;using System.IO;using System.Web;using System.Collections;using System.Collections.Generic;using System.Threading;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Serialization;[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class AutoCompleteService : System.Web.Services.WebService{ private static string[] autoCompleteWordList = null; [WebMethod] public String[] GetWordList(string prefixText, int count) { // init the suggest list if (autoCompleteWordList == null) { string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/WordData.txt")); Array.Sort(temp, new CaseInsensitiveComparer()); // sort for binary search autoCompleteWordList = temp; } int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer()); if (index < 0) { index = ~index; } int matchingCount; for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++) { if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)) break; } String[] returnValue = new string[matchingCount]; if (matchingCount > 0) { Array.Copy(autoCompleteWordList, index, returnValue, 0, matchingCount); } return returnValue; }}
Web Service建立好之后您可以直接测试一下,如果一切正确,我们就继续编写Atlas页面。
首先,无论使用客户端AutoComplete Behavior还是服务器端AutoComplete Extender,一个ScriptManager都是必不可少的:
< atlas:ScriptManager runat ="server" ID ="scriptManager" />
如果使用客户端AutoComplete Behavior,首先需要书写一个HTML input:
< input id ="clientTextBox" type ="text" style ="width: 400px;" />
然后,相应的书写Atlas Script。请小心书写毕竟这里基本没有强大的IDE的支持。
< page xmlns:script ="http://schemas.microsoft.com/xml-script/2005" > < components > < textBox id ="clientTextBox" > < behaviors > < autoComplete serviceURL ="AutoCompleteService.asmx" serviceMethod ="GetWordList" minimumPrefixLength ="2" completionSetCount ="10" completionInterval ="500" /> </ behaviors > </ textBox > </ components > </ page >
在使用服务器端AutoComplete Extender时,一切都非常简单,我们只需要添加一个服务器端TextBox和一个AutoComplete Extender即可:
< asp:TextBox ID ="serverTextbox" runat ="server" Width ="400px" /> < atlas:AutoCompleteExtender ID ="serverCompleteExtender" runat ="server" > < atlas:AutoCompleteProperties Enabled ="true" MinimumPrefixLength ="2" TargetControlID ="serverTextbox" ServiceMethod ="GetWordList" ServicePath ="AutoCompleteService.asmx" /> </ atlas:AutoCompleteExtender >
至此为止,大功告成,让我们在浏览器中测试一下:
客户端AutoComplete Behavior:
服务器端AutoComplete Extender:
本实例程序的源代码可以在此下载: