AjaxControlToolKit之AutoCompleteExtender用法

1、安装完ASPAJAXExtSetup.msi后新建VS2005项目,可以选择AJAX模板,也可不用选择。
2、如没有选择AJAX模板新建网站项目,则需要在配置文件中添加以下代码:
<system.web>
<httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
</system.web>
然后添加AjaxControlToolKit.dll的引用,将控件添加至工具箱。
3、将AjaxControlToolKit.ToolKitScriptManager控件拖至页面中...
4、将TextBox(你将实现自动搜索的文本控件)拖到页面中ID=“myTextBox”...
5、将AjaxControlToolKit.AutoCompleteExtender控件拖至页面中ID=“autoComplete1
6、设置autoComplete1的TargetControlID属性为“myTextBox”...从而实现对myTextBox的控制...
7、新建一个webservice.asmx,命名为"AutoComplete.asmx",代码如下:

// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class AutoComplete : WebService
{
    public AutoComplete()
    {
    }

    [WebMethod]
    public string[] GetCompletionList(string prefixText, int count)
    {
        if (count == 0)
        {
            count = 10;
        }

        if (prefixText.Equals("xyz"))
        {
            return new string[0];
        }

        Random random = new Random();
        List<string> items = new List<string>(count);
        for (int i = 0; i < count; i++)
        {
            char c1 = (char) random.Next(65, 90);
            char c2 = (char) random.Next(97, 122);
            char c3 = (char) random.Next(97, 122);

            items.Add(prefixText + c1 + c2 + c3);
        }

        return items.ToArray();
    }
}
8、最后必须指定一下AutoCompleteExtender控件几个重要属性:
MinimumPrefixLength:指定开始搜索判断的文本长度
ServicePath:指定上面建立的webservice文件路径
ServiceMethod:指定定义的webMethod。



你可能感兴趣的:(autocomplete)