需要AJAX AutoCompleteExtender帮助

时间:2014-07-10 21:41:40

标签: c# asp.net ajax

我有两个文本框,应该填充Ajax AutoCompleteExtender信息。一个文本使用Web服务完成,另一个文本是Web方法背后的代码。 Web Service一个用于客户ID;如果我使用Web服务启动我的页面,并为其工作的ID输入一个数字,并提供所有信息,但如果我尝试以我的实际asp.net形式执行此操作,那么当我输入一个数字时没有任何反应我的搜索条件。另外,对于Web方法而言,当我输入一个字母时,从后面的代码中似乎没有找到任何东西......我想知道我是否缺少一个引用或者某些东西正在使我实际的网页没有获取数据,或者我只是做错了,因为这是我第一次使用它。

我正在使用Visual Studio 2012

asp.net webform

<%@ 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
    }
}

网络服务

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>

1 个答案:

答案 0 :(得分:0)

我认为您的问题在于您的方法签名。如果您有UseContextKey =“True”,则该方法应为:

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

如果UseContextKey =“False”,则方法应为:

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