1. 程式人生 > >AJAX AutoComplete的用法

AJAX AutoComplete的用法

呼叫頁:
//在頁面頂部註冊控制元件
<%@ 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[]{"第一行","第二行"};

        }

    }

}