[英]AJAX AutoComplete in C# asp.net 4.5 Not Working
我正在尝试第一次使用AJAX,但是却一无所获。 我已经阅读了许多网站,据我所知我的代码是正确的,但是当我测试该页面时,没有得到任何结果。
这是我的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" />
这是我的代码背后,位于代码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();
}
}
}
我已经测试过,从不调用GetSearchList,所以它没有调用GetCompletionList。 谁能看到我在做什么错? 我正在Visual Studio Express 2012 for Web中对此进行编程。
为了证明这是一个Web服务问题,请在您的搜索页面代码的后面创建一个page方法,如下所示:
[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();
}
注意:ASP.NET页面方法必须是static
。 另外,您可能需要添加一些using
来使代码得以编译。
现在,您可以在autocompleteextender标记中仅作为方法名称来调用此页面方法,因为它是标记本地的,如下所示:
ServicePath="GetCompletionList"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.