体验AJAX Toolkit新控件:AutoCompleteExtender
AutoCompleteExtender控件,固名思义就是可以辅助TextBox控件自动输入,实现如http://dict.yodao.com/这样的效果。
AutoCompleteExtender控件的属性:
1.TargetControlID:指定将被辅助完成自动输入的控件ID,这里的控件只能是TextBox;
2.ServicePath:指出提供服务的WEB服务路径,若不指出则ServiceMethod表示本页面对应的方法名;
3.ServiceMethod:指出提供服务的方法名;
4.MinimumPrefixLength:指出开始提供提示服务时,TextBox控件应有的最小字符数,默认为3;
5.CompletionSetCount:显示的条数,默认为10;
6.EnableCaching:是否在客户端缓存数据,默认为true;
7.CompletionInterval:从服务器读取数据的时间间隔,默认为1000,单位:毫秒。
ASPX页面代码:
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<ajaxToolkit:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
ServicePath="AutoComplete.asmx"
TargetControlID="TextBox1"
ServiceMethod="GetTextString">
</ajaxToolkit:AutoCompleteExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<ajaxToolkit:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
ServicePath="AutoComplete.asmx"
TargetControlID="TextBox1"
ServiceMethod="GetTextString">
</ajaxToolkit:AutoCompleteExtender>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
WebServcie-AutoComplete.cs代码:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
}
[WebMethod]
public string[] GetTextString(string prefixText, int count)
{
string[] strArray = new string[count];
for (int i = 0; i < count; i++)
{
strArray[i] = prefixText + i.ToString();
}
return strArray;
}
}
在这里需要注意以下几点:using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
}
[WebMethod]
public string[] GetTextString(string prefixText, int count)
{
string[] strArray = new string[count];
for (int i = 0; i < count; i++)
{
strArray[i] = prefixText + i.ToString();
}
return strArray;
}
}
1.由于该WEB服务是为Ajax框架提供服务的,因此在类声明之前得加上属性声明:
[System.Web.Script.Services.ScriptService]
2.特别需要注意的是GetTextString这个方法。凡是为AutoCompleteExtender控件提供服务的方法都必需完全满足以下三个条件:
A.方法的返回类型必需为:string [];
B.方法的传入参数类型必需为:string , int;
C.两个传入参数名必需为:prefixText , count。