原来做asp.net的时候,有些表单是带有参照类型的,比如城市的省份城市的录入,或者员工姓名的录入,以前的做法是走了两个极端,一种是用户在TextBox中输入,另一种是在DropDownList中进行选择。第一种用户需要记住录入的全部内容,输入效率才高,第二种无需提前知道录入内容,但是当供选择的记录过多的时候,选择起来也比较麻烦。那么一种智能式选择是一种折中的做法,我们原来是设置字典参照,然后在字典中选择。现在有了Atlas,这种事情实现起来就简单多了。atlas的AutoCompleteProperties就可以满足这方面的要求。它可以通过设置TargetControlID来控制某个控件,并且需要提供一个Web Services的路径和Web Services的方法。
AutoCompleteProperties的属性包括
下面是一个Demo:
按照上篇文章介绍,创建一个Atlas网站,然后再一个页面中添加如下代码:
下面是处理智能选择的网络服务:
此时,运行效果如下:
这个控件虽然好用易用,但是我思考却不应该滥用。比如在一个很多人并发填写表单的时候,这样每写几个字就调用一下Web Services,每次取回来的东西也不会太大,这对于网络服务来说,连接占用的时间过多,这严重偏离了网络服务大块头设计的原则。因此应用也要看下环境。
AutoCompleteProperties的属性包括
属性名称 | 属性描述 | 备注 |
TargetControlID | 指定要控制的控件的ID | 一般为TextBox的ID |
ServicePath | 处理智能选择列表的Web Services路径 | |
ServiceMethod | 处理智能选择列表的网络服务服务 | 该方法一般包含两个参数(string prefixText, int count) |
Enabled | 是否可用 | |
MinimumPrefixLength | 最小前缀的长度大小 | 当输入长度达到最小的时候,便提供智能选择 |
按照上篇文章介绍,创建一个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 >
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 >
下面是处理智能选择的网络服务:
1
using
System;
2 using System.Web;
3 using System.Collections;
4 using System.Web.Services;
5 using System.Web.Services.Protocols;
6 using System.IO;
7
8
9 /**/ /// <summary>
10/// WebService 的摘要说明
11/// </summary>
12 [WebService(Namespace = " http://tempuri.org/ " )]
13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14 public 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。
2 using System.Web;
3 using System.Collections;
4 using System.Web.Services;
5 using System.Web.Services.Protocols;
6 using System.IO;
7
8
9 /**/ /// <summary>
10/// WebService 的摘要说明
11/// </summary>
12 [WebService(Namespace = " http://tempuri.org/ " )]
13 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14 public 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
此时,运行效果如下:
这个控件虽然好用易用,但是我思考却不应该滥用。比如在一个很多人并发填写表单的时候,这样每写几个字就调用一下Web Services,每次取回来的东西也不会太大,这对于网络服务来说,连接占用的时间过多,这严重偏离了网络服务大块头设计的原则。因此应用也要看下环境。