首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoCompleteExtender AJAX问题

AutoCompleteExtender AJAX问题
EN

Stack Overflow用户
提问于 2011-03-19 01:11:35
回答 1查看 3.5K关注 0票数 0

好的,我在名为autocomplete.asmx (web服务文件)的文件中使用了下面的代码,我的主要问题是,我需要为每个我希望自动完成功能的字段创建不同的web服务吗?也就是说,也许我希望有公司名称,而不是国家,但另一次可能是名称,现在我知道这只是涉及更改select语句,但我如何才能这样做,使它根据它是什么字段,它知道使用什么select语句?

谢谢

代码语言:javascript
复制
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;

}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-19 02:06:38

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

为此,您需要在ajax AutoCompleteExtender控件中使用ContextKey属性

以下是示例代码

标记:

搜索

代码语言:javascript
复制
<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#代码背后的代码:

受保护的空字符串(object ddlType_SelectedIndexChanged,EventArgs e) { string strContextKey = "";

代码语言:javascript
复制
    if(ddlType.SelectedValue.ToString() == "0")
        strContextKey = "Country";
    else
        strContextKey = "Companies";

    AutoCompleteExtender1.ContextKey = ddlType.SelectedItem.Text;
}

WebService代码:

代码语言:javascript
复制
[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; 
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5355488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档