2011-03-18 38 views
0

好的我正在使用下面的代码文件中称为autocomplete.asmx(网络服务文件)我的主要问题是我需要创建一个不同的Web服务,我希望我的汽车完成工作的每个领域? IE浏览器也许我希望将公司名称而不是国家,但另一次也许是名字,现在我知道这只是涉及改变选择声明,但我怎么能做到这一点,以便取决于它是什么字段,它知道什么选择语句使用?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服务的WebMethod来填充国家和公司。

对于要在阿贾克斯AutoCompleteExtender控件

下面使用ContextKey财产是示例代码

标记:

搜索

<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#代码:

保护无效ddlT ype_SelectedIndexChanged(object sender,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; 
}