AutoComplete是对文本框控件的扩展,当用户输入前面的字母时,以弹出区域的形式给出备选词条。就像我们在用百度搜索时可以在下方弹出近似搜索词组一样。
textbox控件,ToolkitScriptManager控件,AutoCompleteExtender控件和示例数据库:本例中是TextFile.txt
前台代码:
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
TargetControlID="TextBox1" ServicePath ="~/AutoComplete.asmx" MinimumPrefixLength ="1"
UseContextKey="True" ServiceMethod="GetCompleteList">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
注意:要设置MinimumPrefixLength,不然输再多的字母也弹不出备选词条。
本例中新建了一个WEB服务:AutoComplete.asmx。
在AutoComplete.asmx中写入下列代码:
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
/// <summary>
///AutoComplete 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[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)
{
//将Text中的文件写入到temp数组
string[] temp = File.ReadAllLines(Server.MapPath("~/App_Data/TextFile.txt"));
//对Temp数组进行排序
Array.Sort(temp, new CaseInsensitiveComparer());
autoCompleteWordList = temp;
//BinarySearch(Array, Object, IComparer)
//如果找到 value,则为指定 array 中的指定 value 的索引。如果找不到 value 且 value 小于 array
//中的一个或多个元素,则为一个负数,该负数是大于 value 的第一个元素的索引的按位求补。
//如果找不到 value 且 value 大于 array 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补
//可对负结果应用按位求补运算符 (~)以生成一个索引。
//如果此索引大于等于数组的大小,则数组中没有比 value 更大的元素。否则,即为大于 value 的第一个元素的索引。
int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());
//indcx<0,即autoCompleteWordList中没有和输入相匹配的数
if (index < 0)
{
index = ~index;
}
//matchingCount;,匹配的行数。
int matchingCount;
for (matchingCount = 0; matchingCount < count && index + matchingCount < autoCompleteWordList.Length;matchingCount ++ )
{
//没有找到以prefixText为开始的字条刚停止
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 ;
}
}
其中用到的几个方法:
1
Sort(Array, IComparer) | 使用指定的 IComparer,对一维 Array 中的元素进行排序。 |
2
BinarySearch(Array, Object, IComparer) | 使用指定的 IComparer 接口,在整个一维排序 Array 中搜索值。 |
参数
-
array
-
类型:System
..::.Array
要搜索的已排序一维 Array。
-
value
-
类型:System
..::.Object
要搜索的对象。
-
comparer
-
类型:System.Collections
..::.IComparer
比较元素时要使用的 IComparer 实现。
- 或 -
若为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则使用每个元素的 IComparable 实现。
返回值
类型:System..::.Int32
如果找到 value,则为指定 array 中的指定 value 的索引。如果找不到 value 且 value 小于 array 中的一个或多个元素,则为一个负数,该负数是大于 value 的第一个元素的索引的按位求补。如果找不到 value 且 value 大于 array 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补。
如果 Array 不包含指定值,则该方法会返回负整数。可对负结果应用按位求补运算符 (~)以生成一个索引。如果此索引大于等于数组的大小,则数组中没有比 value 更大的元素。否则,即为大于 value 的第一个元素的索引。
3
Copy(Array, Int32, Array, Int32, Int32) | 从指定的源索引开始,复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从指定的目标索引开始)。长度和索引指定为 32 位整数。 |
参数
-
sourceArray
-
类型:System
..::.Array
Array,它包含要复制的数据。
-
sourceIndex
-
类型:System
..::.Int32
一个 32 位整数,它表示 sourceArray 中复制开始处的索引。
-
destinationArray
-
类型:System
..::.Array
Array,它接收数据。
-
destinationIndex
-
类型:System
..::.Int32
一个 32 位整数,它表示 destinationArray 中存储开始处的索引。
-
length
-
类型:System
..::.Int32
一个 32 位整数,它表示要复制的元素数目。