ajaxToolKit autoCompleteExtender简单例子

ajaxToolKit中有个autoCompleteExtender对TextBox的扩展,能实现在文本框输入时有下拉框提示。效果不错。使用AjaxToolKit需要安装asp.net的ajax扩展工具:官方文档,然后引用DLL。前台如下:

<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID
="txtID"
ServicePath
="autoCompExtenderServer.asmx"
ServiceMethod
="GetData"
CompletionSetCount
="10"
FirstRowSelected
="true"
MinimumPrefixLength
="1"
>
</asp:AutoCompleteExtender>
其中TargetControlID表示绑定的TextBox

ServicePath:服务路径

ServiceMethod:自动提示的方法名

CompletionSetCount:显示的条数

FirstRowSelected:是否自动选择到提示的第一行

MinimumprefixLength:输入多少个字时开始提示;

autoCompExtenderServer.asmx后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
namespace auotoCompleteText
{
/// <summary>
/// autoCompExtenderServer 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(
false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class autoCompExtenderServer : System.Web.Services.WebService
{

[WebMethod]
public string[] GetData(string prefixText, int count)
{
string[] data = new string[10000];
for (int i = 0; i < data.Length; i++)
data[i]
= i.ToString("0000");
return data.Where(p => p.IndexOf(prefixText) >= 0).Take(count).ToArray();
}
}
}
注意这里的两个参数:prefixText,count都不能改变。

参考资料:http://moosdau.blog.163.com/blog/static/43711282008824113942459/

原文地址:https://www.cnblogs.com/xinjian/p/1886283.html