2013-10-22 65 views
0

我用我的ASP.net頁的AJAX自動完成擴展控制文本框:自動完成擴展不能調用Web服務方法

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

我創建了一個Web服務,並從自動調用此方法控制擴展:

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

如果我單獨運行此WebService它的正常工作,但是當我運行文本框中沒有顯示自動完成選項的項目。

任何人都可以幫助我分析錯誤。

在此先感謝

+0

這是一個古老的問題,但我有同樣的問題,我發現參數必須完全命名。在我的情況下,我使用contextKey以及我的Web服務方法如下所示:[WebMethod(),ScriptMethod()] public static string [] GetCompletionListOther(string prefixText,int count,string contextKey)。這基本上是Ankur在對Monika的回答發表評論時所說的。 – shev72

回答

0

你認爲將WebMethod的簽名是錯誤的,試試這個(中prefixText是必要的,我認爲)

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

使用

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

您已將參數名稱更改爲'PrefixText',但在代碼中它仍是'Prefix' .. items.Add(Prefix + c1 + c2 + c3)' – Ankur

-1

要允許此使用ASP.NET AJAX從腳本調用Web Service,在文件後面的asmx代碼中取消註釋以下行:

[System.Web.Script.Services.ScriptService]