Ajax Control Toolkit AutoCompleteExtender将当前页面的字符html源字符显示为自动完成建议列表

我正在尝试将http://www.aspsnippets.com/Articles/AJAX-AutoCompleteExtender-Example-in-ASPNet.aspx上的autocomplte示例实现到我自己的网页。

作者说;

在这里我要解释一下,如何在不使用任何Web服务的情况下直接在ASP.Net Web页面上使用AJAX AutoCompleteExtender Control。

我有

  1. 下载了AjaxControlToolkit
  2. 安装了工具
  3. 根据我自己的目标编写代码。

我的代码如下:

  ...   ...    //Default.aspx.cs [System.Web.Script.Services.ScriptMethod()] [System.Web.Services.WebMethod] public static List searchInDictionary(string prefixText, int count) { using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = ConfigurationManager .ConnectionStrings["myConnectionString"].ConnectionString; using (OleDbCommand cmd = new OleDbCommand()) { cmd.CommandText = "SELECT word FROM Dictionary WHERE word LIKE @searchTerm + '%'"; cmd.Parameters.AddWithValue("@searchTerm", prefixText); cmd.Connection = conn; conn.Open(); List result = new List(); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { result.Add(dr["word"].ToString()); } } conn.Close(); return result; } } 

在文本框中键入4个字符后,我得到一个列表,其中包含太多的chacarter,它们是当前页面的html源代码。 每行上只有一个源代码字符。 它像是

 < ! D O C T Y P E ... 

直到

  

在此处输入图像描述

我试图自动完成“癌症”这个词。 我键入“canc”,它列出了HTML源代码。

我使用FireBug检查了页面在Net选项卡的XHR部分中,将触发POST操作,其值如下:

JSON

 count 10 prefixText "canc" 

资源

 {"prefixText":"canc","count":10} 

我有

  1. 在当前解决方案中创建了一个Web服务。
  2. 将方法searchInDictionary移动到App_Code文件夹中的服务的.cs文件。

MyDictionary.cs如下:

 /* App_Code/MyDictionary.cs */ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Services; using System.Data.OleDb; using System.Configuration; ///  /// Summary description for MyDictionary ///  [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class MyDictionary : System.Web.Services.WebService { public MyDictionary() { //Uncomment the following line if using designed components //InitializeComponent(); } [ScriptMethod()] [WebMethod] //removed static modifier //display error: Unknown web method searchInDictionary. public List searchInDictionary(string prefixText, int count) { using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = ConfigurationManager .ConnectionStrings["myConnectionString"].ConnectionString; using (OleDbCommand cmd = new OleDbCommand()) { cmd.CommandText = "SELECT word FROM Dictionary WHERE word LIKE @prefixText"; cmd.Parameters.AddWithValue("@prefixText", prefixText + "%"); cmd.Connection = conn; conn.Open(); List result = new List(); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { result.Add(dr["word"].ToString()); } } conn.Close(); return result; } } } } 
  1. 从方法searchInDictionary()中删除了修饰符static 。 因为我收到了错误:

未知的网络方法searchInDictionary。

  1. ServicePath属性添加到cc1:AutoCompleteExtender元素。

新代码:

   
  1. 修改了Default.aspx,用于与Dictionar Web服务建立连接。

添加

 using DictionaryServiceRef; 

现在,它运作良好。 下一个问题是如何链接单词以显示其解释。

将您的webmethod从protected更改为public

 public static List searchInDictionary(string prefixText, int count) { //your code here } 

在将VS 2005中编写的自动完成代码移动到a后,我遇到了同样的问题

VS 2013中的项目。执行以下操作后,它得到了解决:

1)名为“GetSuggestions”的我的ServiceMethod出现在包含自动完成文本框的同一表单的代码中。 我首先向项目添加了一个新的Web服务类(AutoCompleteSample.asmx),并将我的服务方法移动到该类(AutoCompleteSample.asmx.cs)

2)在AutoCompleteExtender控件的属性中,我添加了一个属性ServicePath =“AutoCompleteSample.asmx”

3)取消注释Web服务类定义上方的属性[System.Web.Script.Services.ScriptService],以使条目如下所示:

 [System.Web.Script.Services.ScriptService] public class AutoCompleteSample : System.Web.Services.WebService { 

4)确保用于自动完成的服务方法具有[System.Web.Script.Services.ScriptMethod]属性,使其如下所示:

  [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string[] GetSuggestions(string prefixText, int count) { 

做出上述更改为我解决了问题。