【问题标题】:Search Based on AutoComplete Extender Value in asp.net在 asp.net 中基于 AutoComplete Extender 值的搜索
【发布时间】:2014-12-26 15:48:04
【问题描述】:

我有一个带有 AutoCompelte Extender 的文本框,自动完成文本来自数据库。

我为自动完成列表编写了一个存储过程(其中我使用 crossJoin 绑定了来自两个不同表的两列)。

我的问题是:

当我从自动完成文本框中选择一个值并单击搜索按钮时,按钮后面的我的选择查询没有获取任何值。

我的代码是:

SqlConnection Con = new SqlConnection("DataSource=Localhost;User ID = sa; Password =xxxxx;Initial Catalog =sample;");
SqlCommand cmd = new SqlCommand("Select * from tblCategoryDetails where CategoryName LIKE '"+ TextSearch.Text"' ", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);     
DataTable dt = new DataTable();
da.Fill(dt);

GridView3.DataSource = dt;
GridView3.DataBind();

谁能帮我解决这个问题

【问题讨论】:

  • +放在sql查询最后'" + TextSearch.Text +"'
  • Select * from tblCategoryDe​​tails where CategoryName LIKE '%"+ TextSearch.Text + "%' ,这也不行,我的文本框有两个词要搜索

标签: c# asp.net autocomplete


【解决方案1】:

这不是一个存储过程,而只是一个 sql 命令。

SqlCommand cmd = new SqlCommand("Select * from tblCategoryDe​​tails where CategoryName LIKE '"+ TextSearch.Text"' ", Con);

尝试在 SqlCommand 中加入通配符 %

("Select * from tblCategoryDe​​tails where CategoryName LIKE '%"+ TextSearch.Text + "%'", Con);

【讨论】:

  • 您好这不是程序,对于自动完成扩展器我创建了一个程序
  • 我已经尝试过使用 dis ("Select * from tblCategoryDe​​tails where CategoryName LIKE '%"+ TextSearch.Text + "%'", Con);但没有结果
【解决方案2】:
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> CategoryName(string prefixText, string contextKey)
    {
        //Your Stored Procedure HERE then,
 Your Sql Query where Condition should be 
            where Category Like '%' + @Category  + '%'



       //Return to get List you search
        List<string> Category= new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {

            Category.Add(dt.Rows[i]["Category"].ToString());

        }
        return Category;
    }

在您的标记代码中:在 AutoCompleteExtender 中:ServiceMEthod 应该与您的 webmethod 类相同

   <asp:AutoCompleteExtender ID="AutoComp" runat="server" TargetControlID="txtsrch"
      MinimumPrefixLength="3"   EnableCaching="true" CompletionSetCount="1" CompletionInterval="1"
 OnClientItemSelected="ClientItemSelected" ServiceMethod="CategoryName"  CompletionListCssClass="completionListElement"
 CompletionListHighlightedItemCssClass="AutoComplete_ListItemHilite" CompletionListItemCssClass="listItem"                                OnClientPopulated="ClientPopulated" UseContextKey="true" 
OnClientShowing="clientShowingsrch">
                        </asp:AutoCompleteExtender>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多