AutoCompleteExtender轻易实现搜索智能提示 附带一点小讨论

看到AjaxControlToolkit也支持.net4.0了,于是就在vs2010里,做了一个示例。例子本来不少人都写了没什么复杂的,这里贴出来供朋友们参考。最后有个小疑问探讨一下:

前台代码:

    
      
1 <% @ Page Language = " C# " AutoEventWireup = " true " CodeBehind = " Default.aspx.cs " Inherits = " Search.Default " %>
2
3   <% @ Register Assembly = " AjaxControlToolkit " Namespace = " AjaxControlToolkit " TagPrefix = " asp " %>
4   <! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
5   < html xmlns = " http://www.w3.org/1999/xhtml " >
6   < head runat = " server " >
7 < title > My Search </ title >
8   </ head >
9   < body >
10 < form id = " form1 " runat = " server " >
11 < div >
12 < asp:ScriptManager ID = " ScriptManager1 " runat = " server " >
13 < Services >
14 < asp:ServiceReference InlineScript = " True " Path = " AutoCompleteServices.asmx " />
15 </ Services >
16 </ asp:ScriptManager >
17 </ div >
18 < div align = " center " >
19 < asp:TextBox ID = " txtKey " runat = " server " Width = " 600 " ></ asp:TextBox >
20 < asp:AutoCompleteExtender ID = " AutoCompleteExtender1 " runat = " server " TargetControlID = " txtKey "
21 Enabled = " true " EnableCaching = " true " ServicePath = " AutoCompleteServices.asmx " ServiceMethod = " GetTextString "
22 MinimumPrefixLength = " 2 " CompletionSetCount = " 5 " CompletionInterval = " 10 " >
23 </ asp:AutoCompleteExtender ></ div >
24 </ form >
25   </ body >
26 </ html >

web服务代码:

    
      
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4   using System.Web;
5 using System.Web.Services;
6
7 namespace Search
8 {
9 /// <summary>
10 /// AutoCompleteServices 的摘要说明
11 /// </summary>
12 [System.Web.Script.Services.ScriptService]
13 public class AutoCompleteServices : System.Web.Services.WebService
14 {
15 [WebMethod]
16 public string [] GetTextString( string prefixText, int count)
17 {
18 string [] strArray = new string [count];
19
20 chaonong086Entities db = new chaonong086Entities();
21 using (db)
22 {
23 strArray = (from item in db.PE_Article where item.Title.Contains(prefixText) select item.Title).Take(count).ToArray();
24 }
25 return strArray;
26 }
27 }
28 }
1 <P> </P>

以上我用linq去得到一个数组,从而实现了功能。

起初,一直不出现智能提示,别的朋友说貌似是web服务并未得到触发。后来删掉了web服务里默认存在的hello方法,并删掉了以下两行:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]]

然后智能提示才出现了。这真是一个奇怪的现象。

你可能感兴趣的:(autocomplete)