惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
T
Threatpost
T
Tenable Blog
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
C
Cisco Blogs
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
V
V2EX
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
PCI Perspectives
PCI Perspectives
P
Privacy International News Feed
D
Docker

博客园 - .............

验证码生成器 十年编程无师自通 模仿桌面外观 Web服务是否失去前进目标 “开源”SOA正在改写IT规划方程式 C#实现的18位身份证格式验证算法 - ............. - 博客园 怎样使用Junit Framework进行单元测试的编写 从一个项目谈XP在国内的应用 - ............. - 博客园 AOP是什么? 在C#中使用Conditional元数据attribute来实现Debug代码 - ............. - 博客园 IBM CRM 系统解决方案 HR软件之我见-小议目前HR供应商 Atlas学习手记(11):使用ModalPopup Extender Atlas学习手记(10):使用AlwaysVisibleControl Extender Atlas学习手记(9):异步调用Page Method Atlas学习手记(8):调用本地Web Service简单介绍 Atlas学习手记(7):使用DragOverlayExtender实现拖放功能 Atlas学习手记(6):使用Atlas UpdateProgress控件 Atlas学习手记(5):使用服务端定时控件TimerControl Atlas学习手记(3):由UpdatePanel开始
Atlas学习手记(4):使用AutoComplete Extender实现自动完成功能
............. · 2006-08-03 · via 博客园 - .............

转自 TerryLee技术空间
摘要:自动完成功能在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如下:

<atlas:AutoCompleteExtender runat="server" 

 ID
="autoComplete1">

   
<atlas:AutoCompleteProperties TargetControlID="TextBox1" 

      Enabled
="True" ServicePath="AutoCompleteService.asmx" 

      ServiceMethod
="GetWordList" 

      minimumprefixlength
="1"  />

</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文件夹:

单词库

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

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;


/// <summary>

/// Summary description for AutoCompleteService

/// </summary>


[WebService(Namespace 
= "http://tempuri.org/")]

[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]

public class AutoCompleteService : System.Web.Services.WebService {

    
public AutoCompleteService () {

        
//Uncomment the following line if using designed components 

        
//InitializeComponent(); 

    }


    
private static string[] autoCompleteWordList = null;

    [WebMethod]

    
public String[] GetWordList(string prefixText, int count)

    
{
        
if (autoCompleteWordList == null)

        
{
            
string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"));

            Array.Sort(temp, 
new CaseInsensitiveComparer());

            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;
    }

}

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

<atlas:ScriptManager id="AtlasPage1" runat="server" />

<div class="page"id="links">

 
<div id="content">

   
<h2>AutoComplete server control</h2>

   
<asp:TextBox ID="TextBox1" runat="server" Width="220px"></asp:TextBox>

    
<atlas:AutoCompleteExtender runat="server" 

     ID
="autoComplete1">

       
<atlas:AutoCompleteProperties

          
TargetControlID="TextBox1" 

          Enabled
="True" ServicePath="AutoCompleteService.asmx" 

          ServiceMethod
="GetWordList" 

          minimumprefixlength
="1"  />

    
</atlas:AutoCompleteExtender>     

 
</div>

</div>

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

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