2014-11-26 3 views
0

AJAX AutoCompleteExtender를 사용하여 asp.net 웹 페이지에 자동 완성 기능을 추가하려고합니다. 저장 프로 시저 (MS_SQL)를 통해이 작업을 수행 할 수 있기를 원하지만 예제를 찾을 수 없습니다. WebService를 만들었고 코드를 넣으려고 시도했지만 AJAX를 처음 사용했지만 아무 것도 작동하지 않는 것 같습니다.Ajax 자동 완성 저장 프로 시저 ASP.NET

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 : 당신은 ASMX 웹 서비스를 생성하고 사용자가 설정 한

<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> 
+0

쿼리가 제대로 작동합니까? '%'와일드 카드 문자가 보이지 않습니다. 거기에 넣어야 할 수도 있습니다. – Rohit

+0

http : // www.c-sharpcorner.com/UploadFile/74107f/using-stored-procedure-in-autocomplete-extender-textbox-in-a /' – Rohit

+0

고맙습니다. SQL의 Where 절에 와일드 카드를 추가했습니다. \t \t 'where a_AccomName like @prefixText +'% '' – TLCONE

답변

0

ServiceMethod 속성을 제어하지만 웹 페이지에서 페이지 메서드를 찾고 있음을 의미하는 ServicePath이 아닙니다. 그것을 포함하고 있습니다. ServicePath 속성을 웹 서비스의 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> 
+0

답장을 보내 주셔서 감사합니다. ScriptPath를 추가했습니다. 'ScriptPath = "WebServiceForAjax.asmx"' 아직 채워지지 않습니다. 웹 서비스의 코드에 문제가있을 것이라고 생각합니다. 나에게 처음이다. 나는 온라인에서 예를 들고 그것을 바꿨다. HTML로 스크립트에 대한 링크가 필요할 것입니다. 그렇지만 목록을 채우지는 않습니다. – TLCONE