atlas学习系列之二(AutoCompleteExtender篇)

上一篇:atlas学习系列一(简单体验)
原 来做asp.net的时候,有些表单是带有参照类型的,比如城市的省份城市的录入,或者员工姓名的录入,以前的做法是走了两个极端,一种是用户在 TextBox中输入,另一种是在DropDownList中进行选择。第一种用户需要记住录入的全部内容,输入效率才高,第二种无需提前知道录入内容, 但是当供选择的记录过多的时候,选择起来也比较麻烦。那么一种智能式选择是一种折中的做法,我们原来是设置字典参照,然后在字典中选择。现在有了 Atlas,这种事情实现起来就简单多了。atlas的AutoCompleteProperties就可以满足这方面的要求。它可以通过设置 TargetControlID来控制某个控件,并且需要提供一个Web Services的路径和Web Services的方法。
AutoCompleteProperties的属性包括
属性名称 属性描述 备注
TargetControlID 指定要控制的控件的ID 一般为TextBox的ID
ServicePath 处理智能选择列表的Web Services路径
ServiceMethod 处理智能选择列表的网络服务服务 该方法一般包含两个参数(string prefixText, int count)
Enabled 是否可用
MinimumPrefixLength 最小前缀的长度大小 当输入长度达到最小的时候,便提供智能选择
下面是一个Demo:
按照上篇文章介绍,创建一个Atlas网站,然后再一个页面中添加如下代码:
 1<div>
 2    <asp:Panel ID="Panel1" runat="server" Height="125px" Width="125px">
 3    </asp:Panel>
 4    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:DropDownList ID="DropDownList2"
 5        runat="server">
 6    </asp:DropDownList>
 7    <atlas:AutoCompleteExtender ID="AutoCompleteExetender1" runat="server"  DropDownPanelID="Panel1">
 8    <atlas:AutoCompleteProperties  TargetControlID="TextBox1" Enabled="true" ServicePath="WebService.asmx" ServiceMethod="GetWordList"  MinimumPrefixLength="1" />
 9    </atlas:AutoCompleteExtender>
10    </div>

下面是处理智能选择的网络服务:
 1using System;
 2using System.Web;
 3using System.Collections;
 4using System.Web.Services;
 5using System.Web.Services.Protocols;
 6using System.IO;
 7
 8
 9/// <summary>
10/// WebService 的摘要说明
11/// </summary>

12[WebService(Namespace = "http://tempuri.org/")]
13[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14public class WebService : System.Web.Services.WebService {
15
16    public WebService () {
17
18        //如果使用设计的组件,请取消注释以下行 
19        //InitializeComponent(); 
20    }

21    public  string[] AutoCompleteWordList = null;
22    [WebMethod]
23    public string[] GetWordList(string prefixText, int count)
24    {
25        if (AutoCompleteWordList == null)
26        {
27            string[] tempList = File.ReadAllLines(Server.MapPath("~/App_Data/Words.txt"),System.Text.Encoding.Default);
28            Array.Sort(tempList, new CaseInsensitiveComparer());
29            AutoCompleteWordList = tempList;
30        }

31        int index = Array.BinarySearch(AutoCompleteWordList,prefixText,new CaseInsensitiveComparer());
32        if(index<0)
33        {
34            index=~index;
35        }

36        int matchedCount = 0;
37        for (matchedCount = 0; matchedCount < count&&matchedCount+index<AutoCompleteWordList.Length; matchedCount++)
38        {
39            if (!AutoCompleteWordList[matchedCount + index].StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase))
40            {
41                break;
42            }

43        }

44        string[] returnValue = new string[matchedCount];
45        if (matchedCount > 0)
46        {
47            Array.Copy(AutoCompleteWordList,index, returnValue,0, matchedCount);
48        }

49        return returnValue;
50    }

51
52}

53
54
如果在app_data中的txt文件wors.txt。
此时,运行效果如下:

这 个控件虽然好用易用,但是我思考却不应该滥用。比如在一个很多人并发填写表单的时候,这样每写几个字就调用一下Web Services,每次取回来的东西也不会太大,这对于网络服务来说,连接占用的时间过多,这严重偏离了网络服务大块头设计的原则。因此应用也要看下环 境。
上一篇:atlas学习系列一(简单体验)
作者:jillzhang
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2006-08-20 21:58 Robin Zhang 阅读(3113) 评论(15) 编辑 收藏

 回复 引用 查看   
#1楼 2006-08-20 22:07 aspnetx      
又见一atlas学习系列
不过多看一个人的思路就对其理解深一些
关注中
建议楼主从实际解决方案的角度出发来写学习笔记

 回复 引用 查看   
#2楼 2006-08-21 01:50 Dflying Chen      
应该鼓励啊,不过希望可以更加深入一些,服务器端的控件实在太简单了。
 回复 引用 查看   
#3楼 2006-08-21 09:53 晓峰      
你好~~~~~
我是个atlas的初学者觉的你写的挺好,就是自己一实践就做不出来.能否发一份源码参考一下,先谢谢啦!!!
han_xiao_f@163.com

 回复 引用 查看   
#4楼[楼主] 2006-08-21 10:22 jillzhang      
@晓峰
好,我回家之后,发给你。
@Dflying Chen
呵呵,多谢大虾指点。看了你写的一些系列,自己想实践一下,以后会逐渐增加难度,学习系列,一般有个过程。以后多多指教。可以和你交换个连接么?

 回复 引用   
#5楼 2006-08-21 10:27 bisou[未注册用户]
鼓励
但是真的不想再看到关于这种服务器端控件的文章,直接看帮助就可以了
太简单了 再写就没意思了

 回复 引用 查看   
#6楼 2006-09-07 17:51 天轰穿      
@bisou

来看的人水平各不相同嘛!毕竟不是每个人都有基础!

 回复 引用 查看   
#7楼 2006-09-10 20:57 Dflying Chen      
@jillzhang
很高兴我们大家都能够把自己学习的心得体会写出来~~
谢谢你

 回复 引用 查看   
#8楼[楼主] 2006-09-10 21:22 jillzhang      
@Dflying Chen
我写的比较基础,是学习系列,你的才是真正有水平的。希望以后多多指教。

 回复 引用   
#9楼 2006-09-11 16:40 lileltp[未注册用户]
写出来就是好的.支持.
 回复 引用   
#10楼 2006-09-20 01:38 New Commer[未注册用户]
Support!
 回复 引用 查看   
#11楼 2006-09-26 11:31 天歆      
我输入数据的时候会出现:
The server method 'GetWordList' failed with the following error:
“/”应用程序中的服务器错误。
对象的当前状态使该操作无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 对象的当前状态使该操作无效。
这是一个什么样的错误,怎样解决呢?

 回复 引用   
#12楼 2007-07-16 16:50 罗明超[未注册用户]

@jillzhang

你好,很高兴在这里看到这篇文章,在自我感觉看懂源程序后,用以上的程序代理来动手试试时,却遇到问题

提示:You must have a ScriptManager to use an ExtenderControl.

在我加入一个ScriptManager 后,没在出错,但却没有出现预想的效果.

我对这些技术,正在初学阶段,可否发一份源程序给我.谢谢.

email:250853520@qq.com

 回复 引用 查看   
#13楼[楼主] 2007-07-16 18:13 jillzhang      
@罗明超
兄弟,我这个是atlas测试版的时候作的,现在正式版是否有改动,我也没测试过,等有时间了,在现在的环境下再写一个,到时候,发消息给你