使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)
作者:Dflying Chen (http://dflying.cnblogs.com/)
请同时参考:使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(上)

Word List
access control list (ACL)
ADO.NET
aggregate event
alpha channel
anchoring
antialiasing
application base
application domain (AppDomain)
application manifest
application state
ASP.NET
ASP.NET application services database
ASP.NET mobile controls
ASP.NET mobile Web Forms
ASP.NET page
ASP.NET server control
ASP.NET Web application
assembly
assembly cache
assembly manifest
assembly metadata
assertion (Assert)
association class
ASSOCIATORS OF
asynchronous method
attribute
authentication
authorization
autopostback
bounds
boxing
C#
card
catalog
CCW
chevron
chrome
cHTML
CIM
CIM Object Manager
CIM schema
class
client area
client coordinates
clip
closed generic type
CLR
CLS
CLS-compliant
code access security
code-behind class
code-behind file
code-behind page
COM callable wrapper (CCW)
COM interop
Common Information Model (CIM)
common language runtime
common language runtime host
Common Language Specification (CLS)
common object file format (COFF)
common type system (CTS)
comparison evaluator
composite control
configuration file
connection
connection point
constraint
constructed generic type
constructed type
consumer
container
container control
content page
context
context property
contract
control state
cross-page posting
CTS
custom attribute (Attribute)
custom control

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;
}
}
<atlas:ScriptManager runat="server" ID="scriptManager" />
<input id="clientTextBox" type="text" style="width: 400px;" />
<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>
<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>
请同时参考:使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(上)
让我们通过一个实例来测试上述两种方法:
首先,让我们建立一个词库,提供自动完成的列表。这个词库Copy自Atlas的官方文档示例,是一些.NET的常见术语。将其存为WordData.txt并置于App_Data目录下。
access control list (ACL)
ADO.NET
aggregate event
alpha channel
anchoring
antialiasing
application base
application domain (AppDomain)
application manifest
application state
ASP.NET
ASP.NET application services database
ASP.NET mobile controls
ASP.NET mobile Web Forms
ASP.NET page
ASP.NET server control
ASP.NET Web application
assembly
assembly cache
assembly manifest
assembly metadata
assertion (Assert)
association class
ASSOCIATORS OF
asynchronous method
attribute
authentication
authorization
autopostback
bounds
boxing
C#
card
catalog
CCW
chevron
chrome
cHTML
CIM
CIM Object Manager
CIM schema
class
client area
client coordinates
clip
closed generic type
CLR
CLS
CLS-compliant
code access security
code-behind class
code-behind file
code-behind page
COM callable wrapper (CCW)
COM interop
Common Information Model (CIM)
common language runtime
common language runtime host
Common Language Specification (CLS)
common object file format (COFF)
common type system (CTS)
comparison evaluator
composite control
configuration file
connection
connection point
constraint
constructed generic type
constructed type
consumer
container
container control
content page
context
context property
contract
control state
cross-page posting
CTS
custom attribute (Attribute)
custom control然后创建一个Web Service用来提供建议列表,其中逻辑不多讲了,大概是读入上面的词库并根据输入找出相关的词汇,注意一下GetWordList方法的签名。
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:
