如何让我的自动完成扩展器正常工作?

发布于 2024-07-09 00:16:40 字数 1121 浏览 14 评论 0原文

我试图在这封信中搜索我的代码中的错误,但我自己无法让自动完成扩展器工作。 把招工广告。

这是我的代码:(摘自我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪意 2024-07-16 00:16:40

如何解决此问题:

注释掉您的 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?

耳根太软 2024-07-16 00:16:40

除了 GetCompletionList 方法被错误地声明为 static 之外,它还需要有两个属性: [System.Web.Services.WebMethod][System.Web.Script.Services.ScriptMethod]

所以你的声明应该是这样的:

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

另外你的服务类应该有以下属性:

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

如果您的 GetCompletionList 方法引发异常,自动完成扩展程序也会出现损坏。 为了防止这种情况,您应该在函数代码周围添加一个 try..catch

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

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();
}
じ违心 2024-07-16 00:16:40

我认为您的问题是 GetCompletionList 方法被声明为 static

如果您在调试器会话中仅运行 .asmx 代码(或者如果您已将代码部署到 Web 服务器,则浏览到 .asmx 文件),您应该会看到 Web 服务的可用操作列表。 当我更改 Ajax 控制工具包示例中的代码以将此方法声明为静态时,该操作不再在列表中,并且自动完成扩展器也停止工作。

将方法签名更改为:

public string[] GetCompletionList(string prefixText, int count) 

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) 
人│生佛魔见 2024-07-16 00:16:40

在 ScriptManager 中添加对您的 Web 服务的引用,如

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

下面的参考链接以获取更多信息

gridview 中的 Ajax 自动完成文本框

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

栀子花开つ 2024-07-16 00:16:40

首先,从 Web 方法声明中删除“static”。
其次是在代码块中添加 EnableCaching="true" CompletionSetCount="20"

      <cc1:AutoCompleteExtender  

      </cc1:AutoCompleteExtender> 


希望这能解决您的问题。

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.

素食主义者 2024-07-16 00:16:40

当您创建 Web 服务时,顶部有一行内容:

' 允许使用 ASP.NET 从脚本调用此 Web 服务
AJAX,取消注释以下行。

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

只需取消注释该行:

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

这在 Visual Studio 2010 中发生在我身上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文