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 None.gif < div >
 2 None.gif     < asp:Panel  ID ="Panel1"  runat ="server"  Height ="125px"  Width ="125px" >
 3 None.gif     </ asp:Panel >
 4 None.gif     < asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >< asp:DropDownList  ID ="DropDownList2"
 5 None.gif        runat ="server" >
 6 None.gif     </ asp:DropDownList >
 7 None.gif     < atlas:AutoCompleteExtender  ID ="AutoCompleteExetender1"  runat ="server"   DropDownPanelID ="Panel1" >
 8 None.gif     < atlas:AutoCompleteProperties   TargetControlID ="TextBox1"  Enabled ="true"  ServicePath ="WebService.asmx"  ServiceMethod ="GetWordList"   MinimumPrefixLength ="1"   />
 9 None.gif     </ atlas:AutoCompleteExtender >
10 None.gif     </ div >

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

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

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

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

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

43ExpandedSubBlockEnd.gif        }

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

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

51InBlock.gif
52ExpandedBlockEnd.gif}

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

这 个控件虽然好用易用,但是我思考却不应该滥用。比如在一个很多人并发填写表单的时候,这样每写几个字就调用一下Web Services,每次取回来的东西也不会太大,这对于网络服务来说,连接占用的时间过多,这严重偏离了网络服务大块头设计的原则。因此应用也要看下环 境。
上一篇: atlas学习系列一(简单体验)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值