繁体   English   中英

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

[英]Ajax Autocomplete Stored Procedure ASP.NET

我正在尝试使用AJAX AutoCompleteExtender将自动完成功能添加到asp.net网页。 我希望能够通过存储过程(MS_SQL)做到这一点,但是我似乎找不到一个示例。 我已经创建了一个WebService并尝试将代码放入其中,但是我对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>

您已经创建了一个asmx Web服务,并在控件上设置了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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM