2011-11-14 11 views
1

私はasp.netアプリケーションでautocompleteExenderを使用しています。私はasp.netページにautocompleteextenderを配置し、ajax autocompleteextenderの適切性を割り当てるときに、働いていない。これは私のコードサービスです:Ajaxのオートコンプリートが機能していませんか?

[WebMethod] 
public string[] GetCompletionList(string prefixText) 
{ 
    SqlConnection con=new SqlConnection 
    ("server=******;database=Mydb;user id=***;password=****;"); 
    string sql = "Select productname from F_Product 
     Where productname like '" + prefixText + "%'"; 
    SqlDataAdapter da = new SqlDataAdapter(sql, con); 
    try 
    { 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     string[] items = new string[dt.Rows.Count]; 
     int i = 0; 
     foreach (DataRow dr in dt.Rows) 
     { 
      items.SetValue(dr[0].ToString(), i); 
      i++; 
     } 
     return items; 
    } 
    catch 
    { 
     return null; 
    } 
    finally 
    { 
     con.Close(); 

    } 

これは私のajaxオートコンプリートエクステンダコードです。

<asp:AutoCompleteExtender ID="AutoCompleteExtender1" MinimumPrefixLength="2" 
    TargetControlID ="TextBox1" ServiceMethod="GetCompletionList" 
     ServicePath="~/Autocomplete.asmx" 
     runat="server"> 
    </asp:AutoCompleteExtender> 
    <asp:TextBox ID="TextBox1" runat="server" 
    Width="213px"></asp:TextBox> 

答えて

1

これを試してください。

[WebMethod] 
    public string[] GetCompletionList(string prefixText) 
    { 

     string sql = "Select productname from F_Product Where productname like @prefixText "; 
     SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString); 
     da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%"; 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     string[] items = new string[dt.Rows.Count]; 
     int i = 0; 
     foreach (DataRow dr in dt.Rows) 
     { 
      items.SetValue(dr["productname"].ToString(), i); 
      i++; 
     } 
     return items; 
    } 

あなたはそれが役に立つ見つけた場合、あなたはそれが便利場合

+0

@Anadnd私はasp.netで何かgettngしていませんが、私はserivceを実行しているときに動作しています –

+0

このメソッドを有効にする[System.Web.Script.Services.ScriptService]例えば.. [System.Web.Script .Services.ScriptService] パブリッククラスWebService:System.Web.Services.WebService { – AnandMohanAwasthi

+0

返信いただきありがとうございますが、私はこのwebserivceをどこに置く必要があるのか​​分かりません。 –

0
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
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.Web.Script.Services; 



namespace YourProject 
{ 
    /// <summary> 
    /// Summary description for WebService 
    /// </summary> 
    // [ScriptService] 
    //[System.Web.Script.Services.ScriptService()] 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [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 WebService : System.Web.Services.WebService 
    { 

     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 
     [WebMethod] 
     public string HelloWorld() 
     { 
      return "Hello World"; 
     } 
     [WebMethod] 
     public string[] GetCompletionList(string prefixText) 
     { 

      string sql = "Select productname from F_Product Where productname like @prefixText "; 
      SqlDataAdapter da = new SqlDataAdapter(sql, System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString); 
      da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%"; 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 
      string[] items = new string[dt.Rows.Count]; 
      int i = 0; 
      foreach (DataRow dr in dt.Rows) 
      { 
       items.SetValue(dr["productname"].ToString(), i); 
       i++; 
      } 
      return items; 
     } 
} 
} 

は、あなたの答え他に私に知らせて、それをマークしてください...他にあなたの答えは私が知っているとして、それをマークしてください。..

+0

Mr. AnandMohanAwasthi –

関連する問題