如何让我的自动完成扩展器正常工作?
我试图在这封信中搜索我的代码中的错误,但我自己无法让自动完成扩展器工作。 把招工广告。
这是我的代码:(摘自我的 aspx 页面)
<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>
我的 Web 服务代码:
[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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如何解决此问题:
注释掉您的 SQL 代码。 只需返回一个包含一些测试数据的数组。 那样有用吗? 你看到了吗? 如果没有,您的网络服务代码不会被调用。 如果有效,那么您的问题出在您的数据库代码上...您的 Web 服务代码是否位于调用页面上?
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?
除了
GetCompletionList
方法被错误地声明为static
之外,它还需要有两个属性:[System.Web.Services.WebMethod]
和[System.Web.Script.Services.ScriptMethod]
所以你的声明应该是这样的:
另外你的服务类应该有以下属性:
如果您的
GetCompletionList
方法引发异常,自动完成扩展程序也会出现损坏。 为了防止这种情况,您应该在函数代码周围添加一个try..catch
块As well as the
GetCompletionList
method being incorrectly declared asstatic
, it needs to have two attributes;[System.Web.Services.WebMethod]
and[System.Web.Script.Services.ScriptMethod]
So your declaration should look like this:
Also your service class should have the following attributes:
The autocomplete extender will also appear to be broken if your
GetCompletionList
method throws an exception. To guard against this you should add atry..catch
block around the code the function我认为您的问题是
GetCompletionList
方法被声明为static
。如果您在调试器会话中仅运行 .asmx 代码(或者如果您已将代码部署到 Web 服务器,则浏览到 .asmx 文件),您应该会看到 Web 服务的可用操作列表。 当我更改 Ajax 控制工具包示例中的代码以将此方法声明为静态时,该操作不再在列表中,并且自动完成扩展器也停止工作。
将方法签名更改为:
I think your problem is that the
GetCompletionList
method is declaredstatic
.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:
在 ScriptManager 中添加对您的 Web 服务的引用,如
下面的参考链接以获取更多信息
gridview 中的 Ajax 自动完成文本框
Add reference to your web service in ScriptManager like this
refer link below for more info
Ajax AutoComplete textbox in gridview
首先,从 Web 方法声明中删除“static”。
其次是在代码块中添加 EnableCaching="true" CompletionSetCount="20"
。
希望这能解决您的问题。
First of all, you remove "static" from your web method declaration.
Second is add EnableCaching="true" CompletionSetCount="20" in your
code block.
Hope this will solve your problem.
当您创建 Web 服务时,顶部有一行内容:
只需取消注释该行:
这在 Visual Studio 2010 中发生在我身上。
When you create a Webservice, at the top there is a line that say:
Just uncomment the line:
This happened to me in Visual Studio 2010.