【问题标题】:ajax autocomplete extender is not workingajax 自动完成扩展器不工作
【发布时间】:2013-11-06 21:01:36
【问题描述】:

我在文本框上有一个自动完成扩展器,它将记录显示为来自数据库的列表,但是当我单击 texbox 并开始输入任何内容时。我的html代码是

<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>

我的网络服务是

    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;
    }
   }

【问题讨论】:

  • 能不能把database.asmx的代码也放上来?
  • 我正在通过我的服务方法从数据库中调用对象列表。这是上面给出的。
  • 对不起,我的意思是 Webservice.asmx 文件,只是想确保它是正确的。
  • 好的,我会发布整个文件

标签: asp.net ajax autocomplete


【解决方案1】:

尝试添加这一行,我记得我曾经遇到过同样的问题,它在本地对我有用,但不是在现场。

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

【讨论】:

  • 感谢您回答凯,但在我的情况下不起作用
  • 你有错误处理吗?你知道这个函数实际上正在运行吗?尝试只添加一行代码在服务器上创建一个文件并确保它被创建,这是为了确保该函数完全运行。或者只是调试模式下代码中的断点...
  • 没有错误处理,我想我应该尝试改变ajax控制工具包的包
  • 您是否尝试将整个 GetCompletionList 函数放在您的代码后面的页面中,其中文本框只是为了尝试看看它是否在那里工作?
  • 你也可以像这样组合属性:[WebMethod(), ScriptMethod()]
【解决方案2】:

你应该使用

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

在TextBox1之前

【讨论】:

  • 这个 asp:ToolkitScriptManager 在哪里我找不到任何参考?
  • 只需复制此示例并更改属性并使用。
【解决方案3】:

试试这个签名的 web 方法: public string[] GetCompletionList(string prefixText, int count, string contextKey) 我认为扩展器不会接受除 string[]

之外的任何其他返回类型

【讨论】:

  • 使用上下文键参数是可选的。
【解决方案4】:

就我而言,我不得不在 ASMX 文件中取消注释这一行

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

【讨论】:

    【解决方案5】:

    在我的情况下,下面的 css 类有问题,我在源代码中有它,但没有 z-index 属性。

    <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" 
    ClientIDMode="Static" MinimumPrefixLength="1" ServiceMethod="GetAllStaff" 
    TargetControlID="txtTeacherName" ServicePath="~/webservices/WebService.asmx" 
    CompletionInterval="50" EnableCaching="true" CompletionSetCount="20" 
    ShowOnlyCurrentWordInCompletionListItem="true" 
    CompletionListCssClass="CompletionListCssClass">
    </ajaxToolkit:AutoCompleteExtender>
       
    <style>
    .CompletionListCssClass {
        font-size: 12px;
        color: #000;
        padding: 3px 5px;
        border: 1px solid #999;
        background: #fff;
        width: 300px;
        float: left;
        position: absolute;
        margin-left: 0;
        overflow: auto;
        height: 200px;
        cursor: pointer;
        z-index: 10000001 !important;
    }
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多