2013-06-28 38 views
1

我想第一次使用AJAX,我越來越無處。我已經閱讀了很多網站,並且據我所知,我的代碼是正確的,但是當我測試頁面時,我沒有收到任何結果。在C#asp.net 4.5 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中對此進行編程。

+0

的同時實際上是你'.asmx' Web服務級別作爲您的母版頁或路徑錯誤? –

+0

您是否查看了IIs日誌以查看您是否在不同的URL上請求Web服務? – Hogan

回答

1

爲了證明這是一個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(); 
} 

注:ASP.NET頁面方法必須是static。此外,您可能需要添加幾個using以獲取代碼進行編譯。

現在你可以在你的autocompleteextender標記調用此頁面的方法僅僅是方法的名稱,因爲它是本地的您的標記,像這樣:

ServicePath="GetCompletionList" 
+0

這個工作,我會在這裏使用它,它爲什麼會在這裏工作,而不是爲了未來的知識而不在asmx文件中的任何原因?他們都在同一條路上。 – user2272063

+0

查看Web服務代碼否,但Web服務可能會非常棘手並且很難診斷,而無需實際運行代碼。希望我對你有更好的答案,對不起。 –

+0

好的,至少我知道這不是我正在編寫的代碼問題。謝謝您的幫助。 – user2272063