2014-07-10 112 views
0

我有兩個文本框應該被填充Ajax AutoCompleteExtender信息。一個文本使用Web服務完成,另一個文本是Web方法背後的代碼。 Web服務一個用於客戶ID;如果我使用Web服務啓動我的頁面併爲其添加ID,它會提供所有的信息,但是如果我嘗試在我的實際asp.net窗體中執行此操作,那麼當我鍵入一個數字時就沒有任何反應爲我的搜索條件。此外,從後面的代碼的Web方法似乎沒有找到任何東西,當我鍵入一封信......我想知道如果我缺少一個引用或東西,使我的實際網頁不是獲取數據或者我只是做錯了,因爲這是我第一次使用它。AJAX AutoCompleteExtender幫助

我使用Visual Studio 2012

asp.net的網頁表單

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Orders.aspx.cs" Inherits="TropicalServer.UI.Orders" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <link type="text/css" rel="stylesheet" href="~/AppThemes/TropicalStyles/Orders.css" /> 
    <title>Orders Page</title> 

</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
    <!-- Criteria Bar --> 
     <div> 
      <table> 
       <tr>     
        <td> 
         <asp:Label ID="lblOrderDate" runat="server" Text="Order Date: "></asp:Label> 
        </td> 
        <td> 
         <asp:DropDownList ID="ddlOrderDate" runat="server"></asp:DropDownList> 
        </td> 
        <td> 
         <asp:Label ID="lblCustID" runat="server" Text="Customer ID: "></asp:Label> 
        </td> 
        <td> 
         <asp:TextBox ID="tbCustID" runat="server"></asp:TextBox> 
         <ajaxToolkit:AutoCompleteExtender ID="aceCustID" runat="server" 
          ServicePath="wsOrders.asmx" 
          TargetControlID="tbCustID" 
          MinimumPrefixLength="1" 
          CompletionInterval="100" 
          CompletionSetCount="1" 
          ServiceMethod="GetCustomerID" 
          UseContextKey="true" 
          EnableCaching="true"> </ajaxToolkit:AutoCompleteExtender> 
        </td> 
        <td> 
         <asp:Label ID="lblCustName" runat="server" Text="Customer Name: "></asp:Label> 
        </td> 
        <td> 
         <asp:TextBox ID="tbCustName" runat="server"></asp:TextBox> 
         <ajaxToolkit:AutoCompleteExtender ID="aceCustName" runat="server" 
          TargetControlID="tbCustName" 
          MinimumPrefixLength="1" 
          EnableCaching="true" 
          CompletionInterval="1000" 
          CompletionSetCount="1" 
          UseContextKey="True" 
          ServiceMethod="GetCustomerName"> 
         </ajaxToolkit:AutoCompleteExtender> 
        </td> 
        <td> 
         <asp:Label ID="lblSalesManager" runat="server" Text="Sales Manager: "></asp:Label> 
        </td> 
        <td> 
         <asp:DropDownList ID="ddlSalesManager" runat="server"></asp:DropDownList> 
        </td> 
       </tr> 
      </table> 
     </div> 
     <!-- End Criteria --> 

代碼asp.net背後

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using TropicalServer.DAL; 

namespace TropicalServer.UI 
{ 
    public partial class Orders : System.Web.UI.Page 
    { 
     #region Declerations 
      DALConnection TropConnection; 
     #endregion 

     #region Constructor 
     public Orders() 
     { 
      TropConnection = new DALConnection(); 
     } 
     #endregion 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     #region WebMethod 
     [System.Web.Script.Services.ScriptMethod()] 
     [System.Web.Services.WebMethod] 
     public List<string> GetCustomerName(string prefixText) 
     { 
      DataTable dt = new DataTable(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.CommandText = "spCustName"; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@CustName", prefixText); 
      cmd.Connection = TropConnection.GetConnection(); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      sda.Fill(dt); 
      List<string> CustomerNames = new List<string>(); 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       CustomerNames.Add(dt.Rows[i]["CustName"].ToString()); 
      } 
      return CustomerNames; 
     } 
     #endregion 
    } 
} 

Web服務

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using TropicalServer.DAL; 

namespace TropicalServer 
{ 
    /// <summary> 
    /// Summary description for wsOrders 
    /// </summary> 
    [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 wsOrders : System.Web.Services.WebService 
    { 
     #region Declerations 
     DALConnection TropConnection; 
     #endregion 

     #region Constructor 
     public wsOrders() 
     { 
      TropConnection = new DALConnection(); 
     } 
     #endregion 
     //"SELECT * FROM tblOrder WHERE OrderCustomerNumber LIKE @CustID+'%'" 

     [WebMethod] 
     public List<string> GetCustomerID(string prefixText) 
     { 
      DataTable dt = new DataTable(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.CommandText = "spCustID"; 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.Parameters.AddWithValue("@CustID", prefixText); 
      cmd.Connection = TropConnection.GetConnection(); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      sda.Fill(dt); 
      List<string> CustomerIDs = new List<string>(); 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       CustomerIDs.Add(dt.Rows[i]["OrderCustomerNumber"].ToString()); 
      } 
      return CustomerIDs; 
     } 
    } 
} 

WEB CONFIG

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 

    <appSettings> 
    <add key="TropicalServerConnectionString" value="Initial Catalog=TropicalServer;Data Source=Nicolas-PC\SQLEXPRESS;Integrated Security=true;" /> 
    </appSettings> 
    <connectionStrings> 
    <add name="TropicalServerConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=Nicolas-PC;Initial Catalog=TropicalServer;Integrated Security = true" /> 


    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
     <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> 
     <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> 
     </assemblies> 
    </compilation> 
    <authentication mode="Forms"> 
     <!--<forms loginUrl="~/Account/Login.aspx" timeout="2880" />--> 
    </authentication> 

    <pages> 
     <controls> 
     <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" /> 
     </controls> 
    </pages></system.web> 
</configuration> 

回答

0

我認爲你的問題是你的方法簽名。如果你有UseContextKey = 「真」,該方法應該是:

public static string[] GetCustomerID(string prefixText, int count, string contextKey) 
{ 
} 

如果UseContextKey = 「false」 時,該方法應該是:

public static string[] GetCustomerID(string prefixText, int count) 
{ 
} 
+0

嗯..我曾經嘗試這樣做,仍然一無所獲。當我開始在文本框中輸入內容時,我使用了一箇中斷點,但實際上並沒有打電話。 – NicoF

+0

那麼,至少你知道這不是你的Web服務調用,這是問題。我唯一的其他建議是仔細檢查ServicePath是否正確,並嘗試在AutoCompleteExtender定義中添加Enabled =「True」。如果這些行不通,如果您發佈更新的方法,我會看看是否有任何東西彈出。對方法簽名非常挑剔。 – nebulopathy