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();
}
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?
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();
}
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)
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
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.
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.