Ajax自动完成功能无法在我的页面上运行

时间:2012-02-24 15:15:48

标签: .net ajax

我正在使用Ajax控件套件向搜索文本框添加自动填充功能。我无法在我的母版页上使用PageMethod或webservice工作。我能够创建一个不使用母版页的新页面并且它有效。这是我的代码。

编辑 - 我发现页面方法在母版页中不起作用。我仍然无法调用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;
    }

这是我的网络服务

[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 个答案:

答案 0 :(得分: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>

请参阅http://msdn.microsoft.com/en-us/library/bb628467.aspx

中的“如何:使用配置添加ASP.NET AJAX端点”