1.添加AjaxControlToolkit.dll(本人博客资源中下载)
2.<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> 在页面中引用
3.页面中具体代码
<asp:TextBox ID="txtContactName" runat="server" Width="200px" Height="20px" MaxLength="100"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" Enabled="true" TargetControlID="txtContactName" ServicePath="AutoCompleteService.asmx" ServiceMethod="SelectSearchInfo" CompletionSetCount="10" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>
4.添加AutoCompleteService.asmx 文件
AutoCompleteService.asmx 文件中代码
public class AutoCompleteService : System.Web.Services.WebService {
public AutoCompleteService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] SelectSearchInfo(string prefixText,int count)
{
ContactList[] lst = ContactList.GetContactName(prefixText.Trim());
if (lst.Length > 0)
{
string[] result = new string[lst.Length];
for (int i = 0; i < lst.Length; i++)
{
result[i] = lst[i].CContactName + "; " + lst[i].CCorpName;
}
return result;
}
else
{
string[] result = {"没有该联系人,请先添加联系人信息!"};
return result;
}
}
}
5.页面效果