2013-03-07 94 views
0

我在asp.net 4.0(vs2010)中工作。我正在嘗試實現一個AutoCompleteExtender ajax控件。起初我試圖使用頁面方法,但由於我在用戶控件中使用擴展器,因此無法使用此方法。我花了幾天時間研究並確定我需要使用啓用Ajax的WCF服務。所以我做到了。我脫離了我的應用程序,並創建了一個測試應用程序,它模仿我正在嘗試做的事情,並且它可以工作。我把控制權放在頁面上,指向服務和瞧,它激發了事件並且工作得很好。所以我將代碼移植到我的主應用程序中,並且它不起作用。通過不起作用我的意思是它不會在服務中觸發事件。我懷疑問題是ServicePath,但我嘗試了許多不同的途徑,似乎沒有任何工作。 這裏是從控制的代碼:Ajax AutoCompleteExtender不調用wcf服務

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<ContentTemplate> 
    <div style="float: left"> 
     <asp:Label runat="server">Site Id:</asp:Label><br /> 
     <asp:TextBox ID="txtSiteId" runat="server" AutoPostBack="True"  OnTextChanged="TextChangedEvent"></asp:TextBox> 
    </div> 
    <div style="float: left; width: 200px; padding-left: 20px; margin-right: 5px"> 
     <asp:Label runat="server">Entered Site Id's:</asp:Label><br /> 
     <asp:Repeater ID="rptSiteIds" runat="server" OnItemCommand="rptSiteIds_ItemCommand"> 
      <ItemTemplate> 
       <asp:LinkButton id="lnkRemove" Width="50" runat="server" Text="remove" CommandName="remove"></asp:LinkButton> 
       <asp:Label ID="lblItem" runat="server" Text='<%# Eval("Item") %>'></asp:Label> 
      </ItemTemplate> 
      <SeparatorTemplate> 
       <br> 
      </SeparatorTemplate> 
     </asp:Repeater> 
    </div> 

    <Controls:ConfirmationModal runat="server" ID="MaxSiteIdConfirmation" ModalType="Alert" OnOkClicked="ConfirmationOk_Clicked" 
     Title="Site Id Validation" Message="Too many site id's" 
     Width="300" /> 

    <ajaxToolkit:AutoCompleteExtender 
     runat="server" 
     ID="AutoCompleteExtender" 
     TargetControlID="txtSiteId" 
     CompletionSetCount="10" 
     UseContextKey="True" 
     ServicePath="~\DataServices\WebDataServices.svc" 
     ServiceMethod="GetCompletionList" /> 


</ContentTemplate> 

這裏從服務的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Text; 


namespace Web.Internal.DataServices 
{ 
    [ServiceContract(Namespace = "WebDataServices")] 
    [AspNetCompatibilityRequirements(RequirementsMode =      AspNetCompatibilityRequirementsMode.Allowed)] 
public class WebDataServices 
{ 
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is  WebMessageFormat.Json) 
    // To create an operation that returns XML, 
    //  add [WebGet(ResponseFormat=WebMessageFormat.Xml)], 
    //  and include the following line in the operation body: 
    //   WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
    [OperationContract] 
    public void DoWork() 
    { 
     // Add your operation implementation here 
     return; 
    } 

    // Add more operations here and mark them with [OperationContract] 
    [OperationContract] 
    public string[] GetCompletionList(string prefixText, int count) 
    { 
     //return names; 
     String[] autoCompleteWordList = (from s in CreateList() 
             where s.Contains(prefixText) 
             select s).Take(count).ToArray(); 

     return autoCompleteWordList; 
    } 

    private string[] CreateList() 
    { 
     using (UnitOfWork uow = new UnitOfWork("DataService")) 
     { 
      return new ServiceDetailsBusiness(uow).GetDistinctIds().ToArray(); 
     } 

    } 
} 

}

任何幫助,將不勝感激。

回答

0

你需要做兩件事情:

您的服務方法都需要具有以下屬性的裝飾:

[System.Web.Services.WebMethod()] 
[System.Web.Script.Services.ScriptMethod()] 

更新您的OperationContract的attribure所以它看起來是這樣的:

[OperationContract WebInvoke(Method="POST", ResponseFormat = WebMessageFormat.Json)] 

在方法的return autoCompleteWordList語句前添加以下代碼片段。

if (WebOperationContext.Current != null) 
     WebOperationContext.Current.OutgoingResponse.ContentType = "application/json"; 

此外,請確保您可以從Web瀏覽器正確執行的Web服務調用,並且所有的元數據信息顯示正確。你有沒有添加任何EndPoint信息到你的web.config文件?

讓我知道這是否能解決問題。