How can I get my Autocomplete extender to work?

2019-02-27 10:38发布

I've tried to the letter to search for mistakes in my code, but i can't myself get that autocomplete extender to work. Help wanted.

Here's my code: (excerpt from my aspx page)

  <asp:TextBox ID="TextBox1" Width="120px" runat="server"></asp:TextBox>
    <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1" ServiceMethod="GetCompletionList" ServicePath="SearchAutoComplete.asmx" MinimumPrefixLength="1">
    </cc1:AutoCompleteExtender> 

My Webservice code:

 [WebMethod]
    public static string[] GetCompletionList(string prefixText, int count)
    {
        List<string> returnData = new List<string>();
        MySqlConnection con = new MySqlConnection(Connection.ConnectionString());
        string sql = "select title from blog where title like '%" + prefixText + "%'";
        MySqlCommand cmd = new MySqlCommand(sql, con);
        con.Open();
        MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader.Read())
        {
            returnData.Add(reader["title"].ToString());
        }
        return returnData.ToArray();
    }

6条回答
叛逆
2楼-- · 2019-02-27 11:23

Add reference to your web service in ScriptManager like this

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
 <asp:ServiceReference Path="AutoComplete.asmx" />
 </Services>
 </asp:ScriptManager>

refer link below for more info

Ajax AutoComplete textbox in gridview

查看更多
放我归山
3楼-- · 2019-02-27 11:26

First of all, you remove "static" from your web method declaration. Second is add EnableCaching="true" CompletionSetCount="20" in your

      <cc1:AutoCompleteExtender  

      </cc1:AutoCompleteExtender> 

code block. Hope this will solve your problem.

查看更多
成全新的幸福
4楼-- · 2019-02-27 11:29

When you create a Webservice, at the top there is a line that say:

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

'<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Just uncomment the line:

<System.Web.Script.Services.ScriptService()> _

This happened to me in Visual Studio 2010.

查看更多
甜甜的少女心
5楼-- · 2019-02-27 11:35

As well as the GetCompletionList method being incorrectly declared as static, it needs to have two attributes; [System.Web.Services.WebMethod] and [System.Web.Script.Services.ScriptMethod]

So your declaration should look like this:

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

Also your service class should have the following attributes:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]

The autocomplete extender will also appear to be broken if your GetCompletionList method throws an exception. To guard against this you should add a try..catch block around the code the function

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string[] GetCompletionList(string prefixText, int count)
{
    List<string> returnData = new List<string>();

    try
    {
        // database access code that sets returnData
    }
    catch (Exception ex)
    {
        // log the exception
    }

    return returnData.ToArray();
}
查看更多
混吃等死
6楼-- · 2019-02-27 11:38

I think your problem is that the GetCompletionList method is declared static.

If you run up just the .asmx code in a debugger session (or browse to the .asmx file if you have deployed your code to a webserver) you should see a list of available operations for the web-service. When I change the code in the Ajax control toolkit examples to declare this method as static the operation is no longer in the list and the autocomplete extender also stops working.

Change your method signature to:

public string[] GetCompletionList(string prefixText, int count) 
查看更多
Explosion°爆炸
7楼-- · 2019-02-27 11:39

How to troubleshoot this:

Comment out your SQL code. Just return an array with some test data. Does that work? Do you see it? If not, your webservice code is not getting called. If that works, your problem is with your database code.... Is your webservice code on the calling page?

查看更多
登录 后发表回答