一.安装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;
}
2548

被折叠的 条评论
为什么被折叠?



