2013-10-28 135 views
6

我有一个自动完成扩展器的文本框显示记录作为从数据库中的列表,但我在点击texbox并开始打字什么都不开心。我的HTML代码ajax自动完成扩展器不工作

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
     Enabled="True" TargetControlID="TextBox1" ServicePath="~/WebService.asmx" 
       ServiceMethod="GetCompletionList" 
       MinimumPrefixLength="2" 
       CompletionInterval="1000" 
       EnableCaching="true" 
       CompletionSetCount="20" 
       DelimiterCharacters=";, :" 
       ShowOnlyCurrentWordInCompletionListItem="true" > 
    </asp:AutoCompleteExtender> 

而且我的web服务是

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Services; 
    using System.Data; 
    using MySql.Data.MySqlClient; 
    using System.Configuration; 

    /// <summary> 
    /// Summary description for WebService 
    /// </summary> 
    [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 WebService : System.Web.Services.WebService { 

    public WebService() { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    public static List<string> GetCompletionList(string prefixText, int count) 
    { 
     MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["cn"]); 
     if (con.State == ConnectionState.Closed) 
      con.Open(); 
     MySqlCommand cmd = new MySqlCommand("SELECT gotra FROM tbgotra WHERE gotra LIKE '%" + prefixText + "%'",con); 
     List<string> k = new List<string>(); 
     using (MySqlDataReader sdr = cmd.ExecuteReader()) 
     { 
      while (sdr.Read()) 
      { 
       k.Add(sdr["gotra"].ToString()); 
      } 
     } 
     con.Close(); 
     return k; 
    } 
    } 
+0

你可以把database.asmx代码吗? –

+0

我正在通过我的服务方法调用数据库中的对象列表。这是上面给出的。 –

+0

IM对不起,我的意思是Webservice.asmx文件,只是想确保它是正确的。 –

回答

4

尝试加入这一行,我记得我有同样的问题曾经在那里工作对我来说在本地,但不活。

[WebMethod] 
[System.Web.Script.Services.ScriptMethod] <-- Add this line 
public static List<string> GetCompletionList(string prefixText, int count) 
.... 
+0

谢谢你回答凯,但不在我的情况下工作 –

+0

你有任何错误处理?你知道这个函数实际上正在运行吗?尝试添加一行代码在服务器上创建一个文件,并确保其正在创建,以确保该函数可以运行。或者在调试模式下的代码断点... –

+0

没有错误处理,我想我应该尝试更改ajax控制工具包的包 –

0

你应该在使用

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager> 

的TextBox1的

+0

这是什么asp:ToolkitScriptManager我可以找到没有参考到任何地方? – djack109

+0

只需复制此示例并更改属性并使用。使用上下文关键参数的 – user2178872

0

之前尝试这个签名Web方法: public string[] GetCompletionList(string prefixText, int count, string contextKey) 我想扩展不会接受除了字符串[]

任何其他的返回类型
+0

是可选的。 – Mrudula

0

在我的情况下,我不得不在ASMX文件

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]