关于不使用web服务实现文本框自动完成扩展

来博客园很久了,一直是伸手党,呵呵,现在终于申请了一个账号并开通了博客

下面分享下之前在一个项目里遇到的问题

前段时间在一个项目里要求在文本框内输入要搜索的内容,自动提示与此内容相关的词条

当时在博客园里看到了(http://www.cnblogs.com/insus/archive/2013/03/28/2986217.html)这篇文章觉得挺好,我也就按照这样的方法去做了

在逻辑层我写两个一个类,去跟数据层交互

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Web;
 6 using Bthinking.PlatForm.SystemManage.BLL;
 7 using Bthinking.PlatForm.SystemManage.Model;
 8 
 9 
10 /// <summary>
11 ///ActiveDirectoryInfo 的摘要说明
12 /// </summary>
13 public class ActiveDirectoryInfo
14 {
15     Dim_RessellorInfoBLL bll_RessellorInfo = new Dim_RessellorInfoBLL();
16     public ActiveDirectoryInfo()
17     {
18         //
19         //TODO: 在此处添加构造函数逻辑
20         //
21     }
22     public DataTable GetDisplayName(string prefixText,int count)
23     {
24         Dim_RessellorInfoParameters para_RessellorInfo = new Dim_RessellorInfoParameters();
25         para_RessellorInfo.RessellorName = prefixText;
26         Dim_RessellorInfoCollection ressellorCollection = new Dim_RessellorInfoCollection();
27         ressellorCollection = bll_RessellorInfo.GetAllDim_RessellorInfos(para_RessellorInfo,1,count);
28         DataTable ret = new DataTable();
29         ret = ressellorCollection.ToDataTable();
30         return ret;
31     }
32 
33     public int GetDisplayCount(string prefixText, int count)
34     {
35         Dim_RessellorInfoParameters para_RessellorInfo = new Dim_RessellorInfoParameters();
36         para_RessellorInfo.RessellorName = prefixText;
37         count = bll_RessellorInfo.GetRecordCount(para_RessellorInfo);
38         return count;
39     }
40 }
BLL

把一下代码拉至aspx页面

 <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager" runat="server"></ajaxToolkit:ToolkitScriptManager>

 

在对应控件出引入如下代码

<asp:TextBox ID="TxtRessellorName" runat="server" Height="21px" Width="188px"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender 
ID="AutoCompleteExtender1" runat="server" MinimumPrefixLength = "1"
CompletionInterval = "1000" EnableCaching ="true" CompletionSetCount = "10" TargetControlID="TxtRessellorName" ServiceMethod="GetDisplayUserName"> </ajaxToolkit:AutoCompleteExtender>
这里的ServiceMethod="GetDisplayUserName"对应CS中的方法 红色的部分必须添加
 1    [System.Web.Services.WebMethod]
 2     [System.Web.Script.Services.ScriptMethod]
 3     public static string[] GetDisplayUserName(string prefixText, int count)
 4     {
 5         ActiveDirectoryInfo objActiveDirectoryInfo = new ActiveDirectoryInfo();
 6         count = objActiveDirectoryInfo.GetDisplayCount(prefixText, count);
 7         DataTable dt = objActiveDirectoryInfo.GetDisplayName(prefixText, count);
 8         string[] strArray = new string[count];
 9         int i = 0;
10         foreach (DataRow dataRow in dt.Rows)
11         {
12             //array.Add(dataRow["RessellorName"].ToString());
13             strArray[i] = dataRow["RessellorName"].ToString();
14             i++;
15         }
16 
17         //return (string[])array.ToArray(typeof(string));
18         return strArray;
19     }
20 }

效果如下

总体来说,效果还是不错的,但是对于大数据量的词条筛选可能就显得有点鸡肋了

 
posted @ 2013-07-01 17:46  hEnius  阅读(831)  评论(0编辑  收藏  举报