ATLAS自动完成功能

 一.安装Atlas模板;

二.新建网站,选择“Atlas模板网站”;

三..添加组件--工具箱--添加选项卡Atlas--选择项---.net Framwork组件下浏览microsoft.web.atlas.dll文件(atlas安装文件下下);

四.页面上添加Atlas中的AutoCompleteExtender和ScriptManager控件.源代码处添加红色标注部分:

<atlas:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
            ServiceMethod="GetWordList" ServicePath="AutoCompleteService.asmx">
            <atlas:AutoCompleteProperties
                TargetControlID="tbKey"
                Enabled="True"
                ServicePath="AutoCompleteService.asmx"
                ServiceMethod="GetWordList"
                minimumprefixlength="1" />
        </atlas:AutoCompleteExtender>(GetWordList为AutoCompleteService.asmx中的一方法,tbKey是需要此功能的控件)

四.在App_Code中添加类AutoCompleteService.cs

using System;
using System.IO;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Text;

/// <summary>
/// Summary description for AutoCompleteService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class AutoCompleteService : System.Web.Services.WebService
{
    public AutoCompleteService()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    private static string[] autoCompleteWordList = null;

    [WebMethod]
    public String[] GetWordList(string prefixText, int count)
    {
        if (autoCompleteWordList == null)
        {
            string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"),Encoding.GetEncoding("gb2312"));//words.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;
    }

 
}

App_Code中添加WebService.cs类

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


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

    public WebService () {

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

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
   
}

 

五.项目中添加web服务文件AutoCompleteService.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/AutoCompleteService.cs" Class="AutoCompleteService" %>

六.

 

 protected void btnAdd_Click(object sender, EventArgs e)//tbKey中输入词自动完成,若添加的词词库中没有则添加
    {
        string keyword = this.tbKey.Text;
        this.ListBox1.Items.Add(keyword);

        //若文本列表中没有输入词,则添加
        if (ISout(keyword))
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("~/App_Data/words.txt"), true, Encoding.GetEncoding("gb2312"));
            sw.WriteLine(keyword);
            sw.Close();

        }

      

    }

 

 //判断输入值在文本中是否存在
    public bool ISout(string word)
    {
        string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/words.txt"),Encoding.GetEncoding("gb2312"));
        Array.Sort(temp, new CaseInsensitiveComparer());
        int index = Array.BinarySearch(temp, word, new CaseInsensitiveComparer());
        if ((index >= 0) && (index < temp.Length))
            return false;
        else return true;

    }

 

 

 


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值