需要AJAX AutoCompleteExtender帮助

本文关键字:帮助 AutoCompleteExtender AJAX 需要 | 更新日期: 2023-09-27 18:12:36

我有两个文本框,应该用Ajax AutoCompleteExtender信息填充。一个文本是通过Web服务完成的,另一个文本是Web方法背后的代码。Web服务一个用于客户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
    }
}
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配置

<?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>

需要AJAX AutoCompleteExtender帮助

我认为你的问题是你的方法签名。如果你有UseContextKey="True",方法应该是:

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

如果UseContextKey="False",方法应该是:

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