【问题标题】:AutoCompleteExtender AJAX QuestionAutoCompleteExtender AJAX 问题
【发布时间】:2011-07-18 08:23:17
【问题描述】:

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

谢谢

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;

}

}

【问题讨论】:

    标签: c# asp.net sql ajax autocomplete


    【解决方案1】:

    是的,您可以使用相同的网络服务网络方法来填充国家和公司。

    为此,您想在 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) { 字符串 strContextKey = "";

        if(ddlType.SelectedValue.ToString() == "0")
            strContextKey = "Country";
        else
            strContextKey = "Companies";
    
        AutoCompleteExtender1.ContextKey = ddlType.SelectedItem.Text;
    }
    

    网络服务代码:

    [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; 
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多