ASP.NET AJAX开发文本框自动提示功能
点击次数:95 次 发布日期:2008-11-06 07:55:35 作者:源代码网
|
[url=attachments/200611/04_123834_autocomplete.jpg] [/url]这个功能是通过ASP.NET AJAX AutoCompleteExtender实现的,只需要几行代码即可搞定。 首先需要在页面上添加asp:AutoCompleteExtender标记,别忘了之前要有asp:ScriptManager的声明。& lt;asp:ScriptManager ID="ScriptManager" runat="server" /> 软件开发网 www.mscto.com <asp:AutoCompleteExtender ID="AutoCompleteExtender1" TargetC runat="server" ServiceMethod="GetCompletionList" ServicePath="~/SearchAutoComplete.asmx" MinimumPrefixLength="1" /> 其中TargetControlID为输入网址的文本框的ID,ServicePath为得到网站列表的webservice地址, ServiceMethod即那个webservice中的具体方法,MinimumPrefixLength=1意思是当输入一个字符的时候即开始提 示。 在SearchAutoComplete.asmx中要做的就是从数据库中返回最近查询过的网站,并通过prefixText参数来过滤出只以prefixText开头的网站,这样才能实现逐级提示的功能。SearchAutoComplete.asmx代码如下: [WebMethod] public string[] GetCompletionList(string prefixText, int count) { List<string> list = DataProvider.GetURLList(); foreach (string s in list) { if (s.StartsWith(prefixText)) { list.Add(s); } } return list.ToArray(); } 其中List<string> list是声明的了一个string的范型,这个是.net 2.0中新增的功能,避免了原来使用ArrayList带来的装箱和拆箱的性能消耗。DataProvider.GetURLList()从从数据库返回 所有网站列表,返回类型自然也是List<string>。其他的代码都很简单,相信一看就明白了。 源代码网推荐 源代码网供稿. |

[/url]