Ajax 自动完成存储过程 ASP.NET

本文关键字:存储过程 ASP NET Ajax | 更新日期: 2023-09-27 18:31:58

我正在尝试使用 AJAX 自动完成扩展器向 asp.net 网页添加自动完成功能。我希望能够通过存储过程(MS_SQL)执行此操作,但我似乎找不到示例。我已经创建了一个Web服务并尝试将代码放入其中,但是我是AJAX的新手,似乎没有任何效果。

.SQL:

  IF @Statement='AjaxSearch'
    BEGIN
        SELECT DISTINCT
            a_AccomName
        FROM SB_ACCOMMODATION
        WHERE a_AccomName like @prefixText
    END 

C# (WebserviceForAJAX.asmx.cs) :

  using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using AjaxControlToolkit;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace Atlas
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    //[System.ComponentModel.ToolboxItem(false)]

    public class WebService1 : System.Web.Services.WebService
    {
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> SearchCustomers(string prefixText, int count)
        {
            AtlasInterface conn = new AtlasInterface();
            SqlCommand cmdStoredProcedure = new SqlCommand("ATLAS_ACCOMMODATION", conn.sbConn);
            cmdStoredProcedure.CommandType = CommandType.StoredProcedure;
            cmdStoredProcedure.Parameters.Add("@Statement", SqlDbType.Char).Value = "AjaxSearch";
            cmdStoredProcedure.Parameters.Add("@prefixText", SqlDbType.Char).Value = prefixText;
            conn.sbConn.Open();
            List<string> customers = new List<string>();
            using (SqlDataReader sdr = cmdStoredProcedure.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["a_AccomName"].ToString());
                }
            }
            conn.sbConn.Close();
            return customers;
        }
    }
}

.HTML:

<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender 
    ServiceMethod="SearchCustomers"
    MinimumPrefixLength="1"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>

Ajax 自动完成存储过程 ASP.NET

您已经创建了一个 asmx Web 服务,并且您已经设置了控件上的 ServiceMethod 属性,但未在控件上设置了 ServiceMethod 属性,但未设置ServicePath这意味着它正在包含它的网页中查找页面方法。需要将 ServicePath 属性设置为 Web 服务的 URL。

例如

<cc1:AutoCompleteExtender 
    ServiceMethod="SearchCustomers"
    ServicePath="YourService.asmx"
    MinimumPrefixLength="1"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1" runat="server" FirstRowSelected = "false">
</cc1:AutoCompleteExtender>