?
- 在.net中利用ajax的AutoComplete实现类似google搜索建议的效果
- 1.AutoCompleteExtender属性
- TargetControlID:要实现自动完成功能的控件ID
- ServicePath:????Web?Service的路径
- MinimumPrefixLength:用户输入多少个字母才出现提示
- EnableCaching:是否启用缓存
- CompletionSetCount:显示数据的行数
- 2.简单数据库设计
- 表名:SearchKeywords
- 字段??????类型??说明
-
Id??????int?主键,自增
- Keyword?????varchar?搜索的关键字
-
SearchCount?int?关键字搜索的次数
- 随便填些数据
- 3.SearchKeywords表实体类SearchKeywords.cs
-
using?System;
-
namespace?Ajax.Model
- {
-
????
-
????
-
????
-
????public?class?SearchKeywords
- ????{
-
????????public?SearchKeywords()
-
????????{?}
-
????????#region?Model
-
????????private?int?_id;
-
????????private?string?_keyword;
-
????????private?int?_searchcount;
-
????????
-
????????
-
????????
-
????????public?int?id
- ????????{
-
????????????set?{?_id?=?value;?}
-
????????????get?{?return?_id;?}
- ????????}
-
????????
-
????????
-
????????
-
????????public?string?Keyword
- ????????{
-
????????????set?{?_keyword?=?value;?}
-
????????????get?{?return?_keyword;?}
- ????????}
-
????????
-
????????
-
????????
-
????????public?int?SearchCount
- ????????{
-
????????????set?{?_searchcount?=?value;?}
-
????????????get?{?return?_searchcount;?}
-
????????}
-
????????#endregion?Model
- ????}
- }
- 4.数据层SearchKeywordService.cs,DBHelper类在其他的文章中
-
using?System;
-
using?System.Collections.Generic;
-
using?System.Text;
-
using?System.Collections;
-
using?System.Data;
-
using?Ajax.Model;
-
namespace?Ajax.DAL
- {
-
????public?static?partial?class?SearchKeywordService
- ????{
-
????????public?static?string[]?GetHotSearchKeywords(string?keyword,?int?displaycount)
- ????????{
-
????????????IList<SearchKeywords>?keywords?=?new?List<SearchKeywords>();
-
????????????List<string>?result?=?new?List<string>(displaycount);
-
????????????string?sqlHot?=?"select?top?"+displaycount+"?*?from?SearchKeywords?where?keyword?like?'"+keyword+"%'?order?by?SearchCount?desc";
- ????????????keywords?=?GetSearchKeywordBySql(sqlHot);
-
????????????foreach?(SearchKeywords?item?in?keywords)
- ????????????{
- ????????????????result.Add(item.Keyword);
- ????????????}
-
????????????return?result.ToArray();
- ????????}
-
????????public?static?IList<SearchKeywords>?GetSearchKeywordBySql(string?sqlHot)
- ????????{
-
????????????List<SearchKeywords>?list?=?new?List<SearchKeywords>();
-
????????????try
- ????????????{
- ????????????????DataTable?table?=?DBHelper.GetDataSet(sqlHot);
-
????????????????foreach?(DataRow?row?in?table.Rows)
- ????????????????{
-
????????????????????SearchKeywords?skeyword?=?new?SearchKeywords();
-
????????????????????skeyword.id?=?(int)row["id"];
-
????????????????????skeyword.Keyword?=?(string)row["Keyword"];
-
????????????????????skeyword.SearchCount?=?(int)row["SearchCount"];
- ????????????????????list.Add(skeyword);
- ????????????????}
-
????????????????return?list;
- ????????????}
-
????????????catch?(Exception?e)
- ????????????{
- ????????????????Console.WriteLine(e.Message);
-
????????????????throw?e;
- ????????????}
- ????????}
- ????}
- }
- 5.业务层SearchKeywordManager.cs
-
using?System;
-
using?System.Collections.Generic;
-
using?System.Text;
-
using?Ajax.DAL;
-
namespace?Ajax.BLL
- {
-
????public?class?SearchKeywordManager
- ????{
-
????????public?static?string[]?GetHotSearchKeywords(string?keyword,?int?count)
- ????????{
-
????????????return?SearchKeywordService.GetHotSearchKeywords(keyword,count);
- ????????}
- ????}
- }
- 6.在页面根目录添加文件夹System,在System中添加Web服务页面MyWebService.asmx
- MyWebService.asmx后台代码MyWebService.cs
-
using?System;
-
using?System.Web;
-
using?System.Collections;
-
using?System.Web.Services;
-
using?System.Web.Services.Protocols;
-
using?Ajax.BLL;
-
-
-
-
[WebService(Namespace?=?"http://tempuri.org/")]
- [WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
- [System.Web.Script.Services.ScriptService()]
-
public?class?MyWebService?:?System.Web.Services.WebService?{
-
????public?MyWebService?()?{
-
????????
-
????????
- ????}
-
????[WebMethod]
-
????public?string[]?GetHotSearchByKeywords(string?prefixText,int?count)?{
-
????????return?SearchKeywordManager.GetHotSearchKeywords(prefixText,count);
- ????}
- ????
- }
- 7.新建显示页面AutoComplete.aspx
- 在页面中拖入AJAX?Extensions里的ScriptManager控件
- 添加TextBox控件,修改ID为txtSearch
- 拖入AutoCompleteExtender控件,并修改AutoCompleteExtender控件源码为
- <cc1:AutoCompleteExtender
-
????????????TargetControlID="txtSearch"
-
????????????ServicePath="System/MyWebService.asmx"
-
????????????ServiceMethod="GetHotSearchByKeywords"
-
????????????MinimumPrefixLength="1"?
-
????????????EnableCaching="true"
-
????????????CompletionSetCount="10"
-
????????????ID="AutoCompleteExtender1"?runat="server">
- ????????</cc1:AutoCompleteExtender>
- OK,完美运行
- 数据层文件:DBhelper.cs,SearchKeywordService.cs
- 模型层文件:SearchKeywords.cs
- 业务层文件:SearchKeywordManager.cs
- 表示层页面:AutoComplete.aspx,MyWebService.asmx
- 注意添加引用
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|