使用ASP.NET Atlas AutoComplete Behavior或AutoComplete Extender实现自动完成功能(下)

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

让我们通过一个实例来测试上述两种方法:

首先,让我们建立一个词库,提供自动完成的列表。这个词库Copy自Atlas的官方文档示例,是一些.NET的常见术语。将其存为WordData.txt并置于App_Data目录下。

ContractedBlock.gif ExpandedBlockStart.gif 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

然后创建一个Web Service用来提供建议列表,其中逻辑不多讲了,大概是读入上面的词库并根据输入找出相关的词汇,注意一下GetWordList方法的签名。

ContractedBlock.gif ExpandedBlockStart.gif 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static string[] autoCompleteWordList = null;

    [WebMethod]
    
public String[] GetWordList(string prefixText, int count)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
// init the suggest list
        if (autoCompleteWordList == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            index 
= ~index;
        }


        
int matchingCount;
        
for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
                
break;
        }


        String[] returnValue 
= new string[matchingCount];
        
if (matchingCount > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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:

本实例程序的源代码可以在此下载:http://files.cnblogs.com/dflying/AutoCompleteDemo.zip

转载于:https://www.cnblogs.com/Godblessyou/archive/2010/07/16/1779126.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值