加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

实现自动完成功能

发布时间:2020-12-17 02:35:46 所属栏目:安全 来源:网络整理
导读:? 在.net中利用ajax的AutoComplete实现类似google搜索建议的效果 1.AutoCompleteExtender属性 TargetControlID:要实现自动完成功能的控件ID ServicePath:????Web?Service的路径 MinimumPrefixLength:用户输入多少个字母才出现提示 EnableCaching:是否启用缓
?
  1. 在.net中利用ajax的AutoComplete实现类似google搜索建议的效果
  2. 1.AutoCompleteExtender属性
  3. TargetControlID:要实现自动完成功能的控件ID
  4. ServicePath:????Web?Service的路径
  5. MinimumPrefixLength:用户输入多少个字母才出现提示
  6. EnableCaching:是否启用缓存
  7. CompletionSetCount:显示数据的行数
  8. 2.简单数据库设计
  9. 表名:SearchKeywords
  10. 字段??????类型??说明
  11. Id??????int?主键,自增
  12. Keyword?????varchar?搜索的关键字
  13. SearchCount?int?关键字搜索的次数
  14. 随便填些数据
  15. 3.SearchKeywords表实体类SearchKeywords.cs
  16. using?System;
  17. namespace?Ajax.Model
  18. {
  19. ????///?<summary>
  20. ????///?实体类SearchKeywords?。(属性说明自动提取数据库字段的描述信息)
  21. ????///?</summary>
  22. ????public?class?SearchKeywords
  23. ????{
  24. ????????public?SearchKeywords()
  25. ????????{?}
  26. ????????#region?Model
  27. ????????private?int?_id;
  28. ????????private?string?_keyword;
  29. ????????private?int?_searchcount;
  30. ????????///?<summary>
  31. ????????///?
  32. ????????///?</summary>
  33. ????????public?int?id
  34. ????????{
  35. ????????????set?{?_id?=?value;?}
  36. ????????????get?{?return?_id;?}
  37. ????????}
  38. ????????///?<summary>
  39. ????????///?
  40. ????????///?</summary>
  41. ????????public?string?Keyword
  42. ????????{
  43. ????????????set?{?_keyword?=?value;?}
  44. ????????????get?{?return?_keyword;?}
  45. ????????}
  46. ????????///?<summary>
  47. ????????///?
  48. ????????///?</summary>
  49. ????????public?int?SearchCount
  50. ????????{
  51. ????????????set?{?_searchcount?=?value;?}
  52. ????????????get?{?return?_searchcount;?}
  53. ????????}
  54. ????????#endregion?Model
  55. ????}
  56. }
  57. 4.数据层SearchKeywordService.cs,DBHelper类在其他的文章中
  58. using?System;
  59. using?System.Collections.Generic;
  60. using?System.Text;
  61. using?System.Collections;
  62. using?System.Data;
  63. using?Ajax.Model;
  64. namespace?Ajax.DAL
  65. {
  66. ????public?static?partial?class?SearchKeywordService
  67. ????{
  68. ????????public?static?string[]?GetHotSearchKeywords(string?keyword,?int?displaycount)
  69. ????????{
  70. ????????????IList<SearchKeywords>?keywords?=?new?List<SearchKeywords>();
  71. ????????????List<string>?result?=?new?List<string>(displaycount);
  72. ????????????string?sqlHot?=?"select?top?"+displaycount+"?*?from?SearchKeywords?where?keyword?like?'"+keyword+"%'?order?by?SearchCount?desc";
  73. ????????????keywords?=?GetSearchKeywordBySql(sqlHot);
  74. ????????????foreach?(SearchKeywords?item?in?keywords)
  75. ????????????{
  76. ????????????????result.Add(item.Keyword);
  77. ????????????}
  78. ????????????return?result.ToArray();
  79. ????????}
  80. ????????public?static?IList<SearchKeywords>?GetSearchKeywordBySql(string?sqlHot)
  81. ????????{
  82. ????????????List<SearchKeywords>?list?=?new?List<SearchKeywords>();
  83. ????????????try
  84. ????????????{
  85. ????????????????DataTable?table?=?DBHelper.GetDataSet(sqlHot);
  86. ????????????????foreach?(DataRow?row?in?table.Rows)
  87. ????????????????{
  88. ????????????????????SearchKeywords?skeyword?=?new?SearchKeywords();
  89. ????????????????????skeyword.id?=?(int)row["id"];
  90. ????????????????????skeyword.Keyword?=?(string)row["Keyword"];
  91. ????????????????????skeyword.SearchCount?=?(int)row["SearchCount"];
  92. ????????????????????list.Add(skeyword);
  93. ????????????????}
  94. ????????????????return?list;
  95. ????????????}
  96. ????????????catch?(Exception?e)
  97. ????????????{
  98. ????????????????Console.WriteLine(e.Message);
  99. ????????????????throw?e;
  100. ????????????}
  101. ????????}
  102. ????}
  103. }
  104. 5.业务层SearchKeywordManager.cs
  105. using?System;
  106. using?System.Collections.Generic;
  107. using?System.Text;
  108. using?Ajax.DAL;
  109. namespace?Ajax.BLL
  110. {
  111. ????public?class?SearchKeywordManager
  112. ????{
  113. ????????public?static?string[]?GetHotSearchKeywords(string?keyword,?int?count)
  114. ????????{
  115. ????????????return?SearchKeywordService.GetHotSearchKeywords(keyword,count);
  116. ????????}
  117. ????}
  118. }
  119. 6.在页面根目录添加文件夹System,在System中添加Web服务页面MyWebService.asmx
  120. MyWebService.asmx后台代码MyWebService.cs
  121. using?System;
  122. using?System.Web;
  123. using?System.Collections;
  124. using?System.Web.Services;
  125. using?System.Web.Services.Protocols;
  126. using?Ajax.BLL;
  127. ///?<summary>
  128. ///?MyWebService?的摘要说明
  129. ///?</summary>
  130. [WebService(Namespace?=?"http://tempuri.org/")]
  131. [WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
  132. [System.Web.Script.Services.ScriptService()]
  133. public?class?MyWebService?:?System.Web.Services.WebService?{
  134. ????public?MyWebService?()?{
  135. ????????//如果使用设计的组件,请取消注释以下行?
  136. ????????//InitializeComponent();?
  137. ????}
  138. ????[WebMethod]//Web方法
  139. ????public?string[]?GetHotSearchByKeywords(string?prefixText,int?count)?{
  140. ????????return?SearchKeywordManager.GetHotSearchKeywords(prefixText,count);
  141. ????}
  142. ????
  143. }
  144. 7.新建显示页面AutoComplete.aspx
  145. 在页面中拖入AJAX?Extensions里的ScriptManager控件
  146. 添加TextBox控件,修改ID为txtSearch
  147. 拖入AutoCompleteExtender控件,并修改AutoCompleteExtender控件源码为
  148. <cc1:AutoCompleteExtender
  149. ????????????TargetControlID="txtSearch"
  150. ????????????ServicePath="System/MyWebService.asmx"
  151. ????????????ServiceMethod="GetHotSearchByKeywords"
  152. ????????????MinimumPrefixLength="1"?
  153. ????????????EnableCaching="true"
  154. ????????????CompletionSetCount="10"
  155. ????????????ID="AutoCompleteExtender1"?runat="server">
  156. ????????</cc1:AutoCompleteExtender>
  157. OK,完美运行
  158. 数据层文件:DBhelper.cs,SearchKeywordService.cs
  159. 模型层文件:SearchKeywords.cs
  160. 业务层文件:SearchKeywordManager.cs
  161. 表示层页面:AutoComplete.aspx,MyWebService.asmx
  162. 注意添加引用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读