2014-07-10 66 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