功能:
可以辅助TextBox控件自动输入,如在google中搜索时。
属性:
TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;
ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;
ServiceMethod:指出提供服务的方法名;
MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;
CompletionSetCount:显示的条数,默认为10;
EnableCaching:是否在客户端缓存数据,默认为true;
CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒。
代码实例:
ASPX页面代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AutoComplete server control</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<cc1:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
ServicePath="AutoComplete.asmx"
TargetControlID="TextBox1"
ServiceMethod="GetWordList"
MinimumPrefixLength="1"
EnableCaching ="true"
CompletionSetCount="12"
CompletionInterval="1000">
</cc1:AutoCompleteExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
![](/Images/OutliningIndicators/None.gif)
编写相应的webservices
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class AutoComplete : System.Web.Services.WebService
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public AutoComplete ()
{
![](/Images/OutliningIndicators/InBlock.gif)
//Uncomment the following line if using designed components
//InitializeComponent();
}
![](/Images/OutliningIndicators/InBlock.gif)
private static string[] autoCompleteWordList = null;
![](/Images/OutliningIndicators/InBlock.gif)
[WebMethod]
public String[] GetWordList(string prefixText, int count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (autoCompleteWordList == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"));
Array.Sort(temp, new CaseInsensitiveComparer());
autoCompleteWordList = temp;
}
![](/Images/OutliningIndicators/InBlock.gif)
int index = Array.BinarySearch(autoCompleteWordList, prefixText,
new CaseInsensitiveComparer());
if (index < 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
index = ~index;
}
![](/Images/OutliningIndicators/InBlock.gif)
int matchingCount;
for (matchingCount = 0;
matchingCount < count && index + matchingCount <
autoCompleteWordList.Length;
matchingCount++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!autoCompleteWordList[index +
matchingCount].StartsWith(prefixText,
StringComparison.CurrentCultureIgnoreCase))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
String[] returnValue = new string[matchingCount];
if (matchingCount > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Array.Copy(autoCompleteWordList, index, returnValue, 0,
matchingCount);
}
return returnValue;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
在这里需要注意以下几点:
1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:
[System.Web.Script.Services.ScriptService]
2.特别需要注意的是GetTextString这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
A.方法的返回类型必需为:string [];
B.方法的传入参数类型必需为:string , int;
C.两个传入参数名必需为:prefixText , count。
在App_Data下添加words.txt。
access control list (ACL)
![](/Images/OutliningIndicators/None.gif)
ADO.NET
![](/Images/OutliningIndicators/None.gif)
aggregate event
![](/Images/OutliningIndicators/None.gif)
alpha channel
![](/Images/OutliningIndicators/None.gif)
anchoring
![](/Images/OutliningIndicators/None.gif)
antialiasing
![](/Images/OutliningIndicators/None.gif)
application base
![](/Images/OutliningIndicators/None.gif)
application domain (AppDomain)
![](/Images/OutliningIndicators/None.gif)
application manifest
![](/Images/OutliningIndicators/None.gif)
application state
![](/Images/OutliningIndicators/None.gif)
ASP.NET
![](/Images/OutliningIndicators/None.gif)
ASP.NET application services database
![](/Images/OutliningIndicators/None.gif)
ASP.NET mobile controls
![](/Images/OutliningIndicators/None.gif)
ASP.NET mobile Web Forms
![](/Images/OutliningIndicators/None.gif)
ASP.NET page
![](/Images/OutliningIndicators/None.gif)
ASP.NET server control
![](/Images/OutliningIndicators/None.gif)
ASP.NET Web application
运行结果:
![](/images/cnblogs_com/abcdwxc/a7.GIF)