atlas學習系列之二(AutoCompleteExtender篇)
上一篇:atlas學習系列一(簡單體驗)
原 來做asp.net的時候,有些表單是帶有參照類型的,比如城市的省份城市的錄入,或者員工姓名的錄入,以前的做法是走了兩個極端,一種是用戶在 TextBox中輸入,另一種是在DropDownList中進行選擇。第一種用戶需要記住錄入的全部內容,輸入效率才高,第二種無需提前知道錄入內容, 但是當供選擇的記錄過多的時候,選擇起來也比較麻煩。那么一種智能式選擇是一種折中的做法,我們原來是設置字典參照,然后在字典中選擇。現在有了 Atlas,這種事情實現起來就簡單多了。atlas的AutoCompleteProperties就可以滿足這方面的要求。它可以通過設置 TargetControlID來控制某個控件,并且需要提供一個Web Services的路徑和Web Services的方法。
AutoCompleteProperties的屬性包括
下面是一個Demo:
按照上篇文章介紹,創建一個Atlas網站,然后再一個頁面中添加如下代碼:
下面是處理智能選擇的網絡服務:
此時,運行效果如下:
這 個控件雖然好用易用,但是我思考卻不應該濫用。比如在一個很多人并發填寫表單的時候,這樣每寫幾個字就調用一下Web Services,每次取回來的東西也不會太大,這對于網絡服務來說,連接占用的時間過多,這嚴重偏離了網絡服務大塊頭設計的原則。因此應用也要看下環 境。
上一篇: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 | 最小前綴的長度大小 | 當輸入長度達到最小的時候,便提供智能選擇 |
按照上篇文章介紹,創建一個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>
<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。
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

此時,運行效果如下:
這 個控件雖然好用易用,但是我思考卻不應該濫用。比如在一個很多人并發填寫表單的時候,這樣每寫幾個字就調用一下Web Services,每次取回來的東西也不會太大,這對于網絡服務來說,連接占用的時間過多,這嚴重偏離了網絡服務大塊頭設計的原則。因此應用也要看下環 境。
上一篇:atlas學習系列一(簡單體驗)
作者:jillzhang
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
出處:http://jillzhang.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。



浙公網安備 33010602011771號