AutoCompleteExtender AJAX问题

时间:2011-03-18 17:11:35

标签: c# asp.net sql ajax autocomplete

好的我在名为autocomplete.asmx(web服务文件)的文件中使用下面的代码我的主要问题是我是否需要为我希望自动完成工作的每个字段创建不同的Web服务? IE也许我想把公司名称拉出而不是国家,但另一个时间可能是名字,现在我知道这只涉及更改选择语句但我怎么能这样做,以便取决于它是什么字段,它知道要使用哪个select语句?

由于

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;

}

}

1 个答案:

答案 0 :(得分:0)

是的,您可以使用相同的webservice webmethod来填充国家和公司。

为此你想在ajax 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#代码背后的代码:

protected void ddlType_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; 
}