ajax控件之AutoComplete

作者在 2008-04-18 10:38:10 发布以下内容

控件名:AutoComplete

效果即用途:我们都是在vc下工作的人,vc的智能标签是不是很爽啊……

用法:

属性:
    1、TargetControlID:指定要实现提示功能的控件。
    2、ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的。
    3、ServeiceMethod:写在WebService中的用于提取数据的方法的名字。
    4、MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果。
    5、CompletionSetCount:设置提示数据的行数。
    6、CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。

在网站的根目录下添加一个Web服务,下面是web服务的代码……

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;//首先要走的事情,因为要操作文件,所以要加上这个……


/// <summary>
/// wangxiao 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]  //因为要在客户端调用web服务,所以要加上这句……
public class wangxiao : System.Web.Services.WebService {

    public wangxiao () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    //下面写关于获取数据的方法GetCompleteList
    static string[] autoComlist = null;  //定义静态的方法用来存放获取的数据……
    [WebMethod]  //开始定义了 
    public string[] GetCompleteList(string prefixText, int count)
    {
        if (autoComlist == null)
        {
            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/TextFile.txt"));
            Array.Sort(temp, new CaseInsensitiveComparer());
            autoComlist = temp;
        }
        int index = Array.BinarySearch(autoComlist, prefixText,new CaseInsensitiveComparer());
        if (index < 0)
        {
            index = ~index;
        }
        int matchingCount;
        for (matchingCount = 0; matchingCount < count && index + matchingCount < autoComlist.Length; matchingCount++)
        {
            if (!autoComlist[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
            {
                break;
            }
        }
        string[] returnVale = new string[matchingCount];
        if (matchingCount > 0)
        {
            Array.Copy(autoComlist, index, returnVale, 0, matchingCount);
        }
        return returnVale;
   
    
    }

 

}

前台代码:

   <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
        <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ScriptPath="wangxiao.asmx" ServiceMethod="GetCompleteList" MinimumPrefixLength="2" ServicePath="wangxiao.asmx" CompletionInterval="100" CompletionSetCount="5">
        </cc1:AutoCompleteExtender>
          </div>
    </form>

 

————————————————————
技术日志 | 阅读 2434 次
文章评论,共0条
游客请输入验证码