Ajax Control Toolkit:AutoComplete控件示例
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,matchingCount ); } return returnValue ; } } 其中用到的几个方法: 1
2
参数
返回值类型:System..::.Int32 如果找到 value,则为指定 array 中的指定 value 的索引。如果找不到 value 且 value 小于 array 中的一个或多个元素,则为一个负数,该负数是大于 value 的第一个元素的索引的按位求补。如果找不到 value 且 value 大于 array 中的任何元素,则为一个负数,该负数是(最后一个元素的索引加 1)的按位求补。 如果 Array 不包含指定值,则该方法会返回负整数。可对负结果应用按位求补运算符 (~)以生成一个索引。如果此索引大于等于数组的大小,则数组中没有比 value 更大的元素。否则,即为大于 value 的第一个元素的索引。 3
参数
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |