调用页:
//在页面顶部注册控件
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
//TextBox控件
<asp:TextBox ID="txtTo" runat="server" Width="500px" AutoPostBack="True"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtenderSearch"
MinimumPrefixLength="1"//这是设置输入几个字符才开始调用搜索
CompletionInterval="10"
runat="server"
EnableCaching="true"
ServicePath="AutoCompleteSend.asmx"//这是Web Services的路径
ServiceMethod="GetToAddress"//这是处理搜索的Web Services下的一个方法
TargetControlID="txtTo">//这是对应的TextBox
</cc1:AutoCompleteExtender>
被调用的Web Services:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace MySystem.Email
{
/// <summary>
/// AutoCompleteSend 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]//这个很重要,一定要有
public class AutoCompleteSend : System.Web.Services.WebService
{
[WebMethod(EnableSession=true)]//这是打开支持Session的
[ScriptMethod]//这个很重要,一定要有
public string[] GetToAddress(string prefixText, int count)
{
//这里我就不详细写了,只要返回一个string[]就可以了
return new string[]{"第一行","第二行"};
}
}
}