AJAX 控制工具包自动完成扩展器不起作用

发布于 2024-10-21 00:48:47 字数 1519 浏览 2 评论 0原文

我无法让 AJAX CT 自动完成工作。问题是当我开始在文本框中写入时什么也没有发生。我遇到的第一个问题是当我尝试添加自动完成页面方法时出现错误:“无法创建页面方法“GetCompletionList”...”。然后我尝试手动创建它,但仍然没有任何反应。

这是 AdministracijaOsoba.aspx 代码:

<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox><asp:AutoCompleteExtender
                    ID="AutoCompleteExtender1" runat="server" ScriptPath="" 
                    ServiceMethod="GetCompletionList" ServicePath="AdministracijaOsoba.aspx.cs" 
                    TargetControlID="txtOsoba" UseContextKey="True">
                </asp:AutoCompleteExtender>

这是 AdministracijaOsoba.aspx.cs 代码:

public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();

        var osoba = from o in db.osobas
                    orderby o.osoba_prezime
                    select new { o.osoba_id, person = o.osoba_prezime + " " + o.osoba_ime };

        string[] main = new string[0];

        foreach (var o in osoba)
        {
            if (o.person.StartsWith(prefixText))
            {
                Array.Resize(ref main, main.Length + 1);
                main[main.Length - 1] = o.person.ToString();

                if (main.Length == 15)
                {
                    break;
                }
            }
        }

        Array.Sort(main);
        return main;
    }

请注意,我正在使用 LINQ to Entities。对此的任何帮助将不胜感激。

问候!

I can't get AJAX CT Autocomplete to work. The problem is when I start writing in textbox nothing happens. The frist problem I had experienced was when I tried to Add AutoComplete page method I got an error: "Cannot create page method "GetCompletionList"...". Then I tried creating it manually, but still nothing happens.

Here is the AdministracijaOsoba.aspx code:

<asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox><asp:AutoCompleteExtender
                    ID="AutoCompleteExtender1" runat="server" ScriptPath="" 
                    ServiceMethod="GetCompletionList" ServicePath="AdministracijaOsoba.aspx.cs" 
                    TargetControlID="txtOsoba" UseContextKey="True">
                </asp:AutoCompleteExtender>

Here is the AdministracijaOsoba.aspx.cs code:

public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        PravosudnaAkademijaEntities db = new PravosudnaAkademijaEntities();

        var osoba = from o in db.osobas
                    orderby o.osoba_prezime
                    select new { o.osoba_id, person = o.osoba_prezime + " " + o.osoba_ime };

        string[] main = new string[0];

        foreach (var o in osoba)
        {
            if (o.person.StartsWith(prefixText))
            {
                Array.Resize(ref main, main.Length + 1);
                main[main.Length - 1] = o.person.ToString();

                if (main.Length == 15)
                {
                    break;
                }
            }
        }

        Array.Sort(main);
        return main;
    }

Take a note that I'm using LINQ to Entities. Any help on this would be appreciated.

Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

樱花坊 2024-10-28 00:48:47

您的后台代码应如下所示

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

另外,如果您使用 pagescript 方法,则无需为您的 ajax 扩展程序提供 servicepath 属性。

Your code behind should read like this

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

Also, there is no need of providing the servicepath attribute for your ajax extender if you are using a pagescriptmethod.

音栖息无 2024-10-28 00:48:47

将您的声明更改为:

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
    ServiceMethod="GetCompletionList" 
    ServicePath="AdministracijaOsoba.aspx/GetCompletionList" 
    TargetControlID="txtOsoba" UseContextKey="True">

将其添加到您的 AdministracijaOsoba.aspx.cs 代码中:

[WebMethod]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
  ...
}

Change your declaration to this:

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
    ServiceMethod="GetCompletionList" 
    ServicePath="AdministracijaOsoba.aspx/GetCompletionList" 
    TargetControlID="txtOsoba" UseContextKey="True">

Add this to your AdministracijaOsoba.aspx.cs code:

[WebMethod]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
  ...
}
吹梦到西洲 2024-10-28 00:48:47

这是我的 aspx 页面中的内容:

<asp:AutoCompleteExtender ID="tbSearchName_AutoCompleteExtender" runat="server" 
  DelimiterCharacters="" Enabled="True" ServicePath="" 
  TargetControlID="tbSearchName" ServiceMethod="GetCompletionList" 
  UseContextKey="True" MinimumPrefixLength="2">
</asp:AutoCompleteExtender>

在我的代码隐藏页面中,我有:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
  ArrayList testList = new ArrayList();
  ...
  return (string[])testList.ToArray(typeof(string));
}

Here's what I have in my aspx page:

<asp:AutoCompleteExtender ID="tbSearchName_AutoCompleteExtender" runat="server" 
  DelimiterCharacters="" Enabled="True" ServicePath="" 
  TargetControlID="tbSearchName" ServiceMethod="GetCompletionList" 
  UseContextKey="True" MinimumPrefixLength="2">
</asp:AutoCompleteExtender>

In my code behind page I have:

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
  ArrayList testList = new ArrayList();
  ...
  return (string[])testList.ToArray(typeof(string));
}
寄风 2024-10-28 00:48:47

你的代码几乎是正确的。唯一的问题是服务路径不应以 .aspx.cs 结尾,而只能以 .aspx 结尾。如果扩展程序与方法位于同一页面上,则省略 servicepath

Your code is almost right. The only problem is that the service path shouldnt end with .aspx.cs but only .aspx. If the extender is on the same page as the method then leave out servicepath

南街九尾狐 2024-10-28 00:48:47

我也遇到了同样的问题。我知道这有点晚了,但迟到总比不到好...

这是最终对我有用的设置(包含您的 ID 和名称):

代码隐藏 ( aspx.cs ) :

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethod()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        ...
    } 

代码 ( .aspx ):

    <asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox>
    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" 
        runat="server"
        ServiceMethod="GetCompletionList"
        TargetControlID="txtOsoba"  
        UseContextKey="True">
    </asp:AutoCompleteExtender>

如您所见,您不需要设置 ScriptPath 和 ServicePath 属性,因为这些属性代表...

Web 服务的路径
扩展器将拉出单词\句子
完成情况来自。如果这不是
假设,服务方法应该是
页面方法。

自动完成参考页中对此进行了解释。您已经在代码隐藏中定义了 GetCompletionList() 方法,我目前假设该方法符合“页面方法”的资格。因此,如果我们将该方法放在不同的位置(例如 services.cs 或类似的位置),您似乎只会使用 Path 属性。

I too have been having the same problem. I know this is a bit late, but better late than never...

Here is the setup that finally worked for me (with your IDs and names in place):

Code-Behind ( aspx.cs ):

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethod()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        ...
    } 

Code ( .aspx ):

    <asp:TextBox ID="txtOsoba" runat="server"></asp:TextBox>
    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" 
        runat="server"
        ServiceMethod="GetCompletionList"
        TargetControlID="txtOsoba"  
        UseContextKey="True">
    </asp:AutoCompleteExtender>

As you can see, you don't need to set the ScriptPath and ServicePath properties because these properties represent...

The path to the web service that the
extender will pull the word\sentence
completions from. If this is not
provided, the service method should be
a page method
.

which is explained in the AutoComplete Reference Page. You've got your GetCompletionList() method defined in your Code-Behind, which I am currently assuming qualifies as "a page method". So, it seems you would only use the Path properties if we had the method in a different location such as a services.cs or something of that sort.

灵芸 2024-10-28 00:48:47

删除方法声明中的 static 关键字。

remove the static keyword in your method declaration.

橘虞初梦 2024-10-28 00:48:47

也许您缺少指定 AutoCompleteExtenderMinimumPrefixLength 参数。

Perhaps you are missing specifying MinimumPrefixLength parameter of AutoCompleteExtender.

染年凉城似染瑾 2024-10-28 00:48:47

将 TextBox 的 auto-postback 属性设置为 TRUE

Set auto-postback property of TextBox to TRUE

冷清清 2024-10-28 00:48:47

Ajax 自动完成使用服务调用,因此您可以在 aspx.cs 文件中使用以下代码,注意 System.Web.Services.WebMethodAttribute() 属性,这将使该方法可用于服务调用。

或者,您可以使用任何 ASMX 服务WCF 服务,用于广泛且可靠的服务使用。

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetCompletionList(string prefixText, int count, string contextKey)
{
    return UserControls_phonenumbersearch.GetCompletionList(prefixText, count, contextKey);
}

Ajax autocomplete uses a service call, so you can use the below code in your aspx.cs file, Note the System.Web.Services.WebMethodAttribute() attribute, this will make the method accessible for Service call.

Alternatively you can use any ASMX service or WCF service for extensive and reliable service use.

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetCompletionList(string prefixText, int count, string contextKey)
{
    return UserControls_phonenumbersearch.GetCompletionList(prefixText, count, contextKey);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文