[转]轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender
本章介紹AutoCompleteExtender的使用方法。用過(guò)Google的朋友都會(huì)發(fā)現(xiàn),當(dāng)我們?cè)谒阉骺蜉斎腙P(guān)鍵字的時(shí)候,Google會(huì)自動(dòng)列出相關(guān)關(guān)鍵字提示。那么用Ajax.net也能做到么?答案是:Of course!
第一步:建立AJAX Control Toolkit Website
本例子只需要一個(gè)TextBox控件和一個(gè)AutoCompleteExtender組件。拖進(jìn)設(shè)計(jì)界面后代碼如下所示:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"> </ajaxToolkit:AutoCompleteExtender>第二步:建立WebService
要使用AutoCompleteExtender,我們要通過(guò)WebService傳遞數(shù)據(jù)。在解決方案資源管理器單擊項(xiàng)目-〉添加新項(xiàng)-〉Web服務(wù)。我在這里使用了默認(rèn)的名稱(chēng),大家有需要自己改吧。
WebService.asmx的代碼如下:
using System; using System.Web.Services; using System.Collections.Generic; ? ? ///<summary> /// WebService 的摘要說(shuō)明 ///</summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { ? ??? public WebService () { ? ??????? //如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行 ??????? //InitializeComponent(); ??? } ? ??? [WebMethod] ??? public string[] GetCompleteList(string prefixText, int count) { ??????? char c1, c2, c3; ??????? if (count == 0) ??????????? count = 10; ??????? List<String> list = new List<string>(count); ??????? Random rnd = new Random(); ??????? for (int i = 1; i <= count; i++) ??????? { ??????????? c1 = (char)rnd.Next(65, 90); ??????????? c2 = (char)rnd.Next(97, 122); ??????????? c3 = (char)rnd.Next(97, 122); ??????????? list.Add(prefixText + c1 + c2 + c3); ??????? } ??????? return list.ToArray(); ??? } }注意:加亮部分大家要自行添加。[System.Web.Script.Services.ScriptService]一定要加,它告訴.net runtime這個(gè)WebService被允許從客戶(hù)端調(diào)用。另外System.Collections.Generic是List的命名空間,List可以理解成物件或類(lèi)別的集合,我們可以根據(jù)特殊條件在List里找到我們所需要的值,是.net 2.0新增加的類(lèi)。該函數(shù)的意思是產(chǎn)生三個(gè)隨機(jī)字母,自動(dòng)添加在輸入字符串的后面。另外prefixText和count這兩個(gè)參數(shù)名稱(chēng)要嚴(yán)格遵守拼寫(xiě)格式,原因以前已經(jīng)介紹過(guò)了。
第三步:設(shè)置AutoCompleteExtender
具體設(shè)置代碼如下:?
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ??????? TargetControlID="TextBox1" ??????? CompletionSetCount="12" ??????? EnableCaching="true" ??? ????MinimumPrefixLength="3" ??????? ServicePath="WebService.asmx" ??????? ServiceMethod="GetCompleteList"> </ajaxToolkit:AutoCompleteExtender>TargetControlID是目標(biāo)控件ID,這里是TextBox1。CompletionSetCount是指列表列出輸入提示的數(shù)量。EnableCaching指是否啟用緩存,建議啟動(dòng)。MinimumPrefixLength是指輸入多少個(gè)字符后開(kāi)始列出輸入提示列表。ServicePath是指Web服務(wù)的文件名,這里是WebService.asmx。ServiceMethod是指調(diào)用Web服務(wù)的函數(shù)名,這里是GetCompleteList。
OK,運(yùn)行!效果如下圖:
結(jié)束:
本章介紹了AutoCompleteExtender的使用方法。在本例子中,我只是通過(guò)簡(jiǎn)單的算法產(chǎn)生隨機(jī)字母并添加到輸入字符串后面。這種方法實(shí)用性不大,建議大家在實(shí)際使用時(shí)自行編寫(xiě)更復(fù)雜的算法,讓AutoCompleteExtender產(chǎn)生更實(shí)用的效果。
?
引用地址:http://www.falaosao.net/article.asp?id=165
轉(zhuǎn)載于:https://www.cnblogs.com/sishierfei/archive/2009/11/25/1610585.html
總結(jié)
以上是生活随笔為你收集整理的[转]轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 子页面跳转到父页面指定地方
- 下一篇: c#获取txt,word,excel文档