6

我在文本框上有一个自动完成扩展器,它将记录显示为数据库中的列表,但是当我点击 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;
    }
   }
4

5 回答 5

4

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

[WebMethod]
[System.Web.Script.Services.ScriptMethod] <-- Add this line
public static List<string> GetCompletionList(string prefixText, int count)
....
于 2013-10-28T07:31:27.083 回答
0

你应该使用

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

在 TextBox1 之前

于 2014-01-31T14:51:19.850 回答
0

试试这个 web 方法的签名: public string[] GetCompletionList(string prefixText, int count, string contextKey) 我认为扩展器不会接受除 string[] 之外的任何其他返回类型

于 2014-04-09T10:03:51.797 回答
0

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

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
于 2014-04-21T19:57:19.123 回答
0

在我的情况下,这是下面的 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>
于 2020-09-23T21:03:16.547 回答