C#asp.net 4.5中的AJAX AutoComplete不工作

时间:2022-06-01 18:18:29

I am trying to use AJAX for the first time and I am getting nowhere. I have read numerous websites and as far as I can tell my code is correct but when I test the page I am not getting any results.

我试图第一次使用AJAX,我无处可去。我已经阅读了很多网站,据我所知,我的代码是正确的,但是当我测试页面时,我没有得到任何结果。

Here is my aspx code:

这是我的aspx代码:

<%@ Page Title="Search" Language="C#" MasterPageFile="~/Search.Master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="NEReval.Search" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" EnableViewState="True">

    <ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="True">  
    </ajax:ToolkitScriptManager> 

    <asp:TextBox ID="tbxSearch" runat="server" TabIndex="9" Style="position: absolute; left: 0px; top: 35px" Height="21px" Width="400px"></asp:TextBox>
    <ajax:AutoCompleteExtender   
        ID="AutoCompleteExtender1"   
        TargetControlID="tbxSearch"
        MinimumPrefixLength="1"   
        CompletionSetCount="10"
        ServiceMethod="GetCompletionList"
        ServicePath="AutoCompleteService.asmx"
        runat="server" /> 

And here is my code behind which is in a file called AutoCompleteService.asmx

这里是我的代码,后面是一个名为AutoCompleteService.asmx的文件

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

namespace NEReval
{
    /// <summary>
    /// Summary description for AutoCompleteService
    /// </summary>
    [WebService(Namespace = "http://www.nereval.com/")]
    [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 AutoCompleteService : System.Web.Services.WebService
    {
        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count)
        {
            List<String> Return = SearchList.GetSearchList(HttpContext.Current.Session["sTown"].ToString());

            return (from r in Return where r.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select r).Take(count).ToArray();
        } 
    }
}

I have tested and GetSearchList is never called so it is not calling GetCompletionList. Can anyone see what I am doing wrong? I am programming this in Visual Studio Express 2012 for Web.

我已经测试过并且从未调用过GetSearchList,因此它不会调用GetCompletionList。谁能看到我做错了什么?我在Visual Studio Express 2012 for Web中编程。

1 个解决方案

#1


1  

To prove this is a web service issue, in your search page code-behind create a page method, like this:

要证明这是一个Web服务问题,在您的搜索页面中代码隐藏创建一个页面方法,如下所示:

[WebMethod]
public static string[] GetCompletionList()
{
    List<String> Return = SearchList.GetSearchList(HttpContext.Current.Session["sTown"].ToString());

    return (from r in Return where r.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select r).Take(count).ToArray();
}

Note: ASP.NET Page Methods must be static. Also you will probably need to add a few usings to get the code to compile.

注意:ASP.NET页面方法必须是静态的。此外,您可能需要添加一些使用来获取要编译的代码。

Now you can call this page method in your autocompleteextender markup as just the method name, because it is local to your markup, like this:

现在,您可以在autocompleteextender标记中将此页面方法称为方法名称,因为它是您的标记的本地方法,如下所示:

ServicePath="GetCompletionList"

#1


1  

To prove this is a web service issue, in your search page code-behind create a page method, like this:

要证明这是一个Web服务问题,在您的搜索页面中代码隐藏创建一个页面方法,如下所示:

[WebMethod]
public static string[] GetCompletionList()
{
    List<String> Return = SearchList.GetSearchList(HttpContext.Current.Session["sTown"].ToString());

    return (from r in Return where r.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select r).Take(count).ToArray();
}

Note: ASP.NET Page Methods must be static. Also you will probably need to add a few usings to get the code to compile.

注意:ASP.NET页面方法必须是静态的。此外,您可能需要添加一些使用来获取要编译的代码。

Now you can call this page method in your autocompleteextender markup as just the method name, because it is local to your markup, like this:

现在,您可以在autocompleteextender标记中将此页面方法称为方法名称,因为它是您的标记的本地方法,如下所示:

ServicePath="GetCompletionList"