Atlas学习手记(4):使用AutoComplete Extender实现自动完成功能

本文介绍如何使用Atlas的AutoCompleteExtender实现自动完成功能,包括配置WebService提供数据支持及客户端控件设置。

摘要:自动完成功能在Ajax时代已经见的很多了,像Google Suggest,很多邮箱中也都有应用。在Atlas对于自动完成功能提供了很好的支持,提供了客户端的AutoComplete Behavior和服务器端的AutoComplete Extender的支持。本文主要看一下使用AutoComplete Extender来实现自动完成功能。

 

主要内容

1AutoComplete Extender介绍

2.一个完整的示例

 

一.AutoComplete Extender介绍

自动完成功能在Ajax时代已经见的很多了,像Google Suggest,很多邮箱中也都有应用,如下图:

Atlas对于自动完成功能提供了很好的支持,提供了客户端的AutoComplete Behavior和服务器端的AutoComplete Extender的支持。本文主要介绍一下使用AutoComplete Extender来实现自动完成功能。一个简单的AutoComplete Extender如下:

None.gif < atlas:AutoCompleteExtender  runat ="server"  
None.gif
None.gif ID
="autoComplete1" >
None.gif
None.gif   
< atlas:AutoCompleteProperties  TargetControlID ="TextBox1"  
None.gif
None.gif      Enabled
="True"  ServicePath ="AutoCompleteService.asmx"  
None.gif
None.gif      ServiceMethod
="GetWordList"  
None.gif
None.gif      minimumprefixlength
="1"    />
None.gif
None.gif
</ atlas:AutoCompleteExtender >

对于AutoComplete Extender来说,它具有如下属性:

属性

描述

ServicePath

指定自动完成功能Web Service的路径

ServicePath="AutoCompleteService.asmx"

ServiceMethod

指定自动完成功能Web Method的名称

ServiceMethod="GetWordList"

DropDownPanelID

指定显示列表的PanelID,一般情况会提供一个默认的,我们无需指定

minimumprefixlength

开始提供自动完成列表的文本框内最少的输入字符数量。

minimumprefixlength="1"

我们需要为AutoComplete Extender控件指定一个AutoCompleteProperties子控件,它同样具有如下属性:

属性

描述

ServicePath

AutoComplete ExtenderServicePath

ServiceMethod

AutoComplete ExtenderServiceMethod

minimumprefixlength

AutoComplete Extenderminimumprefixlength

Enabled

是否开启自动完成功能,默认值为false

Enabled="True"

TargetControlID

指定自动完成功能应用到哪个TextBox上,设置Web服务器TextBox控件的ID

TargetControlID="TextBox1"

下面通过一个例子来看一下具体如何使用,来自于Atlas的官方网站。

二.一个完整的示例

1.准备相关的数据源,这里使用一个本文文件作为我们的数据源,当然你也可以从数据库中读数据,拷贝如下单词为words.txt并保存在App_Data文件夹:

ContractedBlock.gif ExpandedBlockStart.gif 单词库
None.gifaccess control list (ACL)
None.gif
None.gifADO.NET
None.gif
None.gifaggregate event
None.gif
None.gifalpha channel
None.gif
None.gifanchoring
None.gif
None.gifantialiasing
None.gif
None.gifapplication base
None.gif
None.gifapplication domain (AppDomain)
None.gif
None.gifapplication manifest
None.gif
None.gifapplication state
None.gif
None.gifASP.NET
None.gif
None.gifASP.NET application services database
None.gif
None.gifASP.NET mobile controls
None.gif
None.gifASP.NET mobile Web Forms
None.gif
None.gifASP.NET page
None.gif
None.gifASP.NET server control
None.gif
None.gifASP.NET Web application
None.gif
None.gifassembly
None.gif
None.gifassembly cache
None.gif
None.gifassembly manifest
None.gif
None.gifassembly metadata
None.gif
None.gifassertion (Assert)
None.gif
None.gifassociation class
None.gif
None.gifASSOCIATORS OF
None.gif
None.gifasynchronous method
None.gif
None.gifattribute
None.gif
None.gifauthentication
None.gif
None.gifauthorization
None.gif
None.gifautopostback
None.gif
None.gifbounds
None.gif
None.gifboxing
None.gif
None.gifC#
None.gif
None.gifcard
None.gif
None.gifcatalog
None.gif
None.gifCCW
None.gif
None.gifchevron
None.gif
None.gifchrome
None.gif
None.gifcHTML
None.gif
None.gifCIM
None.gif
None.gifCIM Object Manager
None.gif
None.gifCIM schema
None.gif
None.gifclass
None.gif
None.gifclient area
None.gif
None.gifclient coordinates
None.gif
None.gifclip
None.gif
None.gifclosed generic type
None.gif
None.gifCLR
None.gif
None.gifCLS
None.gif
None.gifCLS-compliant
None.gif
None.gifcode access security
None.gif
None.gifcode-behind class
None.gif
None.gifcode-behind file
None.gif
None.gifcode-behind page
None.gif
None.gifCOM callable wrapper (CCW)
None.gif
None.gifCOM interop
None.gif
None.gifCommon Information Model (CIM)
None.gif
None.gifcommon language runtime
None.gif
None.gifcommon language runtime host
None.gif
None.gifCommon Language Specification (CLS)
None.gif
None.gifcommon object file format (COFF)
None.gif
None.gifcommon type system (CTS)
None.gif
None.gifcomparison evaluator
None.gif
None.gifcomposite control
None.gif
None.gifconfiguration file
None.gif
None.gifconnection
None.gif
None.gifconnection point
None.gif
None.gifconstraint
None.gif
None.gifconstructed generic type
None.gif
None.gifconstructed type
None.gif
None.gifconsumer
None.gif
None.gifcontainer
None.gif
None.gifcontainer control
None.gif
None.gifcontent page
None.gif
None.gifcontext
None.gif
None.gifcontext property
None.gif
None.gifcontract
None.gif
None.gifcontrol state
None.gif
None.gifcross-page posting
None.gif
None.gifCTS
None.gif
None.gifcustom attribute (Attribute)
None.gif
None.gifcustom control

2.编写Web Service用来提供单词列表,这里我们并不关心具体的实现逻辑,只关心Web Method接收什么样的参数,最后返回一个string数组即可。

None.gif using  System;
None.gif
None.gif
using  System.IO;
None.gif
None.gif
using  System.Web;
None.gif
None.gif
using  System.Collections;
None.gif
None.gif
using  System.Collections.Generic;
None.gif
None.gif
using  System.Threading;
None.gif
None.gif
using  System.Web.Services;
None.gif
None.gif
using  System.Web.Services.Protocols;
None.gif
None.gif
using  System.Xml.Serialization;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
InBlock.gif
/// Summary description for AutoCompleteService
InBlock.gif
ExpandedBlockEnd.gif
/// </summary>

None.gif
None.gif[WebService(Namespace 
=   " http://tempuri.org/ " )]
None.gif
None.gif[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  AutoCompleteService : System.Web.Services.WebService  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public AutoCompleteService () dot.gif{
InBlock.gif
InBlock.gif        
//Uncomment the following line if using designed components 
InBlock.gif
InBlock.gif        
//InitializeComponent(); 
InBlock.gif

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private static string[] autoCompleteWordList = null;
InBlock.gif
InBlock.gif    [WebMethod]
InBlock.gif
InBlock.gif    
public String[] GetWordList(string prefixText, int count)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (autoCompleteWordList == null)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"));
InBlock.gif
InBlock.gif            Array.Sort(temp, 
new CaseInsensitiveComparer());
InBlock.gif
InBlock.gif            autoCompleteWordList 
= temp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
int index = Array.BinarySearch(autoCompleteWordList, prefixText,
InBlock.gif
InBlock.gif          
new CaseInsensitiveComparer());
InBlock.gif
InBlock.gif        
if (index < 0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            index 
= ~index;
ExpandedSubBlockEnd.gif        }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        String[] returnValue 
= new string[matchingCount];
InBlock.gif
InBlock.gif        
if (matchingCount > 0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Array.Copy(autoCompleteWordList, index, returnValue, 
0,
InBlock.gif
InBlock.gif              matchingCount);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return returnValue;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

3.添加AutoComplete Extender,首先需要添加一个ScriptManager,再添加一个服务器端的控件和一个AutoComplete Extender,并设置相关的参数,代码如下:

None.gif < atlas:ScriptManager  id ="AtlasPage1"  runat ="server"   />
None.gif
None.gif
< div  class ="page" id ="links" >
None.gif
None.gif 
< div  id ="content" >
None.gif
None.gif   
< h2 > AutoComplete server control </ h2 >
None.gif
None.gif   
< asp:TextBox  ID ="TextBox1"  runat ="server"  Width ="220px" ></ asp:TextBox >
None.gif
None.gif    
< atlas:AutoCompleteExtender  runat ="server"  
None.gif
None.gif     ID
="autoComplete1" >
None.gif
None.gif       
< atlas:AutoCompleteProperties
None.gif
None.gif          
TargetControlID ="TextBox1"  
None.gif
None.gif          Enabled
="True"  ServicePath ="AutoCompleteService.asmx"  
None.gif
None.gif          ServiceMethod
="GetWordList"  
None.gif
None.gif          minimumprefixlength
="1"    />
None.gif
None.gif    
</ atlas:AutoCompleteExtender >      
None.gif
None.gif 
</ div >
None.gif
None.gif
</ div >

好了,到这里整个步骤就全部完成了,运行后就可以看到效果如下:

完整示例下载:http://terrylee.cnblogs.com/Files/Terrylee/AutoCompleteExtenderDemo.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值