2011-03-18 12 views
0

私はautocomplete.asmx(Webサービスファイル)と呼ばれるファイルで以下のコードを使用しています。私の主な質問は、私は自分のオートコンプリートをしたいすべてのフィールドに対して別のWebサービスを作成する必要がありますか?私はおそらく私は会社名が国の代わりに引き出されたいと思うかもしれませんが、別の時におそらく名前、今私はこれがちょうど選択ステートメントを変更することを知っているが、どのようなフィールドに応じて、使用するselectステートメントを知っていますか?AutoCompleteExtender AJAX質問

おかげ

public class AutoComplete : System.Web.Services.WebService 

{ 

[WebMethod] 

public string[] GetCountriesList(string prefixText) 

{ 

    DataSet dtst = new DataSet(); 

    SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); 

    string strSql = "SELECT CountryName FROM Tbl_ooo WHERE CountryName LIKE '" + prefixText + "%' "; 

    SqlCommand sqlComd = new SqlCommand(strSql, sqlCon); 

    sqlCon.Open(); 

    SqlDataAdapter sqlAdpt = new SqlDataAdapter(); 

    sqlAdpt.SelectCommand = sqlComd; 

    sqlAdpt.Fill(dtst); 

    string[] cntName = new string[dtst.Tables[0].Rows.Count]; 

    int i = 0; 

    try 

    { 

     foreach (DataRow rdr in dtst.Tables[0].Rows) 

     { 

      cntName.SetValue(rdr["CountryName"].ToString(), i); 

      i++; 

     } 

    } 

    catch { } 

    finally 

    { 

     sqlCon.Close(); 

    } 

    return cntName; 

} 

} 

答えて

0

はい、あなたは国や企業を取り込むために、同じWebサービスのWebメソッドを使用することができます。

検索

<asp:TextBox ID="txtSearch" CssClass="textBlackBold" runat="server"  Width="350px"></asp:TextBox>         
<asp:DropDownList ID="ddlType" runat="server"        AutoPostBack="True" onselectedindexchanged="ddlType_SelectedIndexChanged">     
           <asp:ListItem Value="0">Country</asp:ListItem> 
           <asp:ListItem Value="1">Companies</asp:ListItem> 
</asp:DropDownList> 
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" 
       CompletionListCssClass="autocomplete_completionListElement" 
       CompletionListItemCssClass="autocomplete_listItem" 
       CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" 
       EnableCaching="true" ContextKey="Products" UseContextKey="true" 
       TargetControlID="txtSearch" MinimumPrefixLength="1" 
       ServiceMethod="GetInfo" ServicePath="~/WebService.asmx" > 
      </asp:AutoCompleteExtender> 

コードC#のコードの後ろ:

あなたは、AJAX AutoCompleteExtender制御に

以下

をContextKeyプロパティを使用することについては

は、サンプルコード

マークアップです

protected void ddlT ype_SelectedIndexChanged(オブジェクト送信者、EventArgs e) { string strContextKey = "";

if(ddlType.SelectedValue.ToString() == "0") 
     strContextKey = "Country"; 
    else 
     strContextKey = "Companies"; 

    AutoCompleteExtender1.ContextKey = ddlType.SelectedItem.Text; 
} 

WebServiceのコード:

[WebMethod] 
public string[] GetInfo(string prefixText, string contextKey) 
{ 
    DataSet dtst = new DataSet();       
    SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); 
    string strSql = ""; 

    if (contextKey == "Country") 
    {       
     strSql = "SELECT CountryName FROM Tbl_ooo WHERE CountryName LIKE '" + prefixText + "%' ";    
    } 
    else if(contextKey == "Companies") 
    { 
     strSql = //Other SQL Query 
    } 

    SqlCommand sqlComd = new SqlCommand(strSql, sqlCon);       
    sqlCon.Open();       
    SqlDataAdapter sqlAdpt = new SqlDataAdapter(); 
    sqlAdpt.SelectCommand = sqlComd;       
    sqlAdpt.Fill(dtst);       
    string[] cntName = new string[dtst.Tables[0].Rows.Count];       
    int i = 0;       
    try       
    {        
     foreach (DataRow rdr in dtst.Tables[0].Rows)        
     {         
      cntName.SetValue(rdr[0].ToString(),i); 
      i++;        
     }       
    } 
    catch { }       
    finally       
    { 
     sqlCon.Close();       
    }       
    return cntName; 
} 
関連する問題