ASP.NET Ajax中AutoComplete控件的使用
创始人
2024-03-20 08:01:03
0

简介

AutoComplete控件就是在用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。

重要属性

1.TargetControlID:指定要实现提示功能的控件;

2.ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的;

3.ServeiceMethod:写在WebService中的用于提取数据的方法的名字;

4.MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果;

5.CompletionSetCount:设置提示数据的行数;

6.CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。

示例

打开vs2005创建一个AjaxControlToolKit网站。

在网站的App_Data文件夹下添加文本文件TextFile.txt,并在其中添加数据,如下:

在网站的根目录下添加一个Web服务,命名为oec2003_AutoComplete,系统自动将Web服务两个部分,设计部分oec2003_AutoComplete.asmx和代码部分oec2003_AutoComplete.cs,其中oec2003_AutoComplete.cs文件自动放入到App_Code目录下。打开oec2003_AutoComplete.cs文件,添加获取数据的方法GetCompleteList,代码如下:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;

 
/// <summary>
/// AutoComplete 的摘要说明
/// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

    public AutoComplete () {

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

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    /// <summary>
    /// 获取数据的方法GetCompleteList
    /// <summary>
    //定义静态数组用于保存获取的数据
    private static string[] autoCompleteWordList = null;
    [WebMethod]
    public String[] GetCompleteList(string prefixText, int count)
    {
        if (autoCompleteWordList == null)
        {
            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/TextFile.txt"));
            Array.Sort(temp, new CaseInsensitiveComparer());
            autoCompleteWordList = temp;
        }

        int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
        if (index < 0)
        {
            index = ~index;
        }

        int matchingCount;
        for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length; matchingCount++)
        {
            if (!autoCompleteWordList[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
            {
                break;
}
        }
        String[] returnValue = new string[matchingCount];
        if (matchingCount > 0)
       {
           Array.Copy(autoCompleteWordList, index, returnValue, 0, matchingCount);
        }
        return returnValue;
    }

}

由于在上面的代码中使用了File类,所以应该添加如下代码:

using System.IO;

因为需要在客户端调用Web服务,还需要添加如下代码:

[System.Web.Script.Services.ScriptService]

保存Web 服务的代码

打开根目录下默认生成的Default.aspx

在页面中拖拽一个TextBox控件和一个AutoCompleteExtender控件。

在属性窗口设置AutoCompleteExtender控件的属性,如下

<ajaxToolkit:AutoCompleteExtender 
            ID="AutoCompleteExtender1" 
            runat="server" 
            ServiceMethod="GetCompleteList" 
            ServicePath="oec2003_AutoComplete.asmx" 
            Enabled="true" 
            MinimumPrefixLength="2" 
               CompletionSetCount="10"
            TargetControlID="TextBox1">
</ajaxToolkit:AutoCompleteExtender>

在Web服务中的count参数的值是取CompletionSetCount属性的值。

保存设计的页面,将默认页面设置为起始页,按F5运行后在文本框中输入oe,就能看到想要的结果。

【编辑推荐】

  1. ASP.NET MVC案例教程
  2. ASP.NET MVC教程:创建TaskList应用程序
  3. ASP.NET中URL Rewrite的实现方法

相关内容

热门资讯

PHP新手之PHP入门 PHP是一种易于学习和使用的服务器端脚本语言。只需要很少的编程知识你就能使用PHP建立一个真正交互的...
网络中立的未来 网络中立性是什... 《牛津词典》中对“网络中立”的解释是“电信运营商应秉持的一种原则,即不考虑来源地提供所有内容和应用的...
各种千兆交换机的数据接口类型详... 千兆交换机有很多值得学习的地方,这里我们主要介绍各种千兆交换机的数据接口类型,作为局域网的主要连接设...
粉嫩如何诠释霸道 东芝M805... “霸道粉”是个什么玩意东芝M805拿过来的时候,笔者扑哧笑了,不是笑这款笔记本,而是笑这款产品的颜色...
什么是大数据安全 什么是大数据... 在《为什么需要大数据安全分析》一文中,我们已经阐述了一个重要观点,即:安全要素信息呈现出大数据的特征...
全面诠释网络负载均衡 负载均衡的出现大大缓解了服务器的压力,更是有效的利用了资源,提高了效率。那么我们现在来说一下网络负载...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
30分钟搞定iOS自定义相机 最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以。就花时间简单研究...
P2P的自白|我不生产内容,我... 现在一提起P2P,人们就会联想到正在被有关部门“围剿”的互联网理财服务。×租宝事件使得劳...