2012-02-24 48 views
0

我正在使用Ajax控件工具箱向我的搜索文本框添加自動完成功能。我無法使用我的主頁上的PageMethod或Web服務來運行它。我能夠創建一個不使用母版頁的新頁面,並且工作。這是我的代碼。Ajax自動完成功能無法在我的頁面上工作

編輯 - 我發現頁面方法不起作用的母版頁。儘管如此,我仍然無法調用Web服務。我添加一個服務引用,然後將其添加到擴展器「ServicePath =」NameWebService.asmx「」我仍然沒有得到任何東西。我檢查一下,看看服務器端是否被擊中,而不是。

<asp:ScriptManager ID ="ScriptManager2" EnableCdn="true" LoadScriptsBeforeUI="false" 
     runat="server" EnablePageMethods="true" /> 
<asp:Panel ID="pnlSearch" runat="server" DefaultButton="btnSearch"> 

        <asp:TextBox ID="txtSearch" CssClass="textbox" placeholder="User Name/ Full Name" 
         runat="server" Width="200px" AutoComplete="off" /> 
        <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
         TargetControlID="txtSearch" ServiceMethod="GetSearchResult" 
         MinimumPrefixLength="2" /> 
        <asp:Button ID="btnSearch" runat="server" CssClass="buttonSubmit" Text="Search" 
         onclick="btnSearch_Click" /> 

       </asp:Panel> 

這是我的第方法

[System.Web.Services.WebMethod] 
    public static string[] GetSearchResult(string prefixText, int count) 
    { 
     List<Person> listPerson = Person.SearchForPerson(prefixText); 
     string[] arrayNames = listPerson.Select(n => n.FullName).Take(count).ToArray(); 

     AbuseReport report = new AbuseReport(); 
     report.AbuserPersonID = -1; 
     report.Message = prefixText; 
     report.ReportingPersonID = -1; 
     report.CreateAbuseReport(); 

     return arrayNames; 
    } 

這是我的web

[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 NameSearch : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public string[] GetSearchResult(string prefixText, int count) 
    { 
     List<Person> listPerson = Person.SearchForPerson(prefixText); 
     string[] arrayNames = listPerson.Select(n => n.FullName).Take(count).ToArray(); 

     AbuseReport report = new AbuseReport(); 
     report.AbuserPersonID = -1; 
     report.Message = prefixText; 
     report.ReportingPersonID = -1; 
     report.CreateAbuseReport(); 

     return arrayNames; 
    } 
} // End of Service 

回答

1

你可能需要在web.config中註冊您的WebMethod

<system.serviceModel> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="ServiceAspNetAjaxBehavior"> 
     <enableWebScript/> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"> 
    <baseAddressPrefixFilters> 
     <add prefix="http://www.yourdomain.com"/> 
    </baseAddressPrefixFilters> 
    </serviceHostingEnvironment> 
    <services> 
    <service name="Service"> 
     <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service"/> 
    </service> 
    </services> 
    <bindings> 
</system.serviceModel> 

見「如何:使用配置添加一個n ASP.NET AJAX端點「在http://msdn.microsoft.com/en-us/library/bb628467.aspx

相關問題