【问题标题】:Auto complete Extender not invoking the Web service method自动完成扩展器不调用 Web 服务方法
【发布时间】:2013-10-22 11:24:21
【问题描述】:

我在我的 ASP.net 页面中使用 ajax 自动完成扩展器控件作为文本框:

<td align="left>
  <asp:TextBox ID="txtAutoComplete" runat="server" MaxLength="200" Width="50%">         </asp:TextBox>

 <asp:AutoCompleteExtender ID="txtAutoComplete_AutocompleteExtender" runat="server"
                        Enabled="true" TargetControlID="txtAutoComplete" UseContextKey="true" ServiceMethod="GetItemsList" ServicePath="~/AutoCompleteWebService.asmx"
                      MinimumPrefixLength="3" CompletionSetCount="12" DelimiterCharacters=";" OnClientPopulating="ShowProcessImage" OnClientPopulated="HideProcessImage">
                    </asp:AutoCompleteExtender>
</td>

我创建了一个网络服务并从自动控制扩展器中调用了这个方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestChart
{
    /// <summary>
    /// Summary description for AutoCompleteWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 AutoCompleteWebService : System.Web.Services.WebService
    {
        public AutoCompleteWebService(){
        }
        [WebMethod]
        public  string[] GetItemsList(string Prefix,int count)
        {
            char c1;
            char c2;
            char c3;`enter code here`

            if (count == 0)
            {
                count = 10;
            }
             Random rnd =new Random();
             List<string> items = new List<string>();
             for (int i = 0; i < count; i++)
             {
                 c1 = Convert.ToChar(rnd.Next(65, 90));
                 c2 = Convert.ToChar(rnd.Next(97, 122));
                 c3 = Convert.ToChar(rnd.Next(97, 122));
                 items.Add(Prefix + c1 + c2 + c3);
             }
                 return items.ToArray();
        }
    }
}

如果我单独运行这个网络服务,它工作正常,但是当我运行项目时,文本框没有显示自动完成选项。

谁能帮我分析一下错误。

提前致谢

【问题讨论】:

  • 这是一个老问题,但我遇到了同样的问题,我发现参数必须准确命名。在我的情况下,我也使用 contextKey,我的 Web 服务方法如下所示:[WebMethod(), ScriptMethod()] public static string[] GetCompletionListOther(string prefixText, int count, string contextKey)。这基本上是 Ankur 在评论 Monika 的回答时所说的。

标签: c# asp.net web-services


【解决方案1】:

你觉得webmethod的签名不对,试试这个(prefixText是必须的,我觉得)

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

【讨论】:

    【解决方案2】:

    使用

    [WebMethod]
            public  string[] GetItemsList(string PrefixText,int count)
            {
                char c1;
                char c2;
                char c3;`enter code here`
    
                if (count == 0)
                {
                    count = 10;
                }
                 Random rnd =new Random();
                 List<string> items = new List<string>();
                 for (int i = 0; i < count; i++)
                 {
                     c1 = Convert.ToChar(rnd.Next(65, 90));
                     c2 = Convert.ToChar(rnd.Next(97, 122));
                     c3 = Convert.ToChar(rnd.Next(97, 122));
                     items.Add(PrefixText + c1 + c2 + c3);
                 }
                     return items.ToArray();
            }
    

    【讨论】:

    • 您已将参数名称更改为PrefixText,但在代码中仍为Prefix..items.Add(Prefix + c1 + c2 + c3)
    【解决方案3】:

    要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释 asmx 代码隐藏文件中的以下行:

    [System.Web.Script.Services.ScriptService]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      相关资源
      最近更新 更多