AJAX自动完成在C#asp.net 4.5中不工作 [英] AJAX AutoComplete in C# asp.net 4.5 Not Working
问题描述
我想使用AJAX的第一次,我越来越行不通。我看过无数的网站和据我可以告诉我的code是正确的,但是当我测试页面,我没有得到任何结果。
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.
下面是我的aspx code:
Here is my aspx code:
<%@ 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" />
这是我的code后面是在一个名为AutoCompleteService.asmx文件
And here is my code behind which is in a file called 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防爆$ P $ 2012 PSS这个编程的Web。
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.
推荐答案
要证明这是一个Web服务的问题,在你的搜索页面code-背后创建一个页面的方法,就像这样:
To prove this is a web service issue, in your search page code-behind create a page method, like this:
[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页面方法必须是静态
。你也可能需要添加一些使用
s到获得code编译。
Note: ASP.NET Page Methods must be static
. Also you will probably need to add a few using
s to get the code to compile.
现在你可以在你autocompleteextender标记调用此页面的方法仅仅是方法的名称,因为它是本地的您的标记,像这样的:
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:
ServicePath="GetCompletionList"
这篇关于AJAX自动完成在C#asp.net 4.5中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!