简体   繁体   English

在asp.net中基于自动完成扩展程序值进行搜索

[英]Search Based on AutoComplete Extender Value in asp.net

I have a textBox with AutoCompelte Extender, Autocomplete Text is from Database. 我有一个带有AutoCompelte Extender的textBox,自动填充文本来自数据库。

I had Written a Stored Proc for Autocomplete List(In Which I m binding Two Columns from Two Different Tables with crossJoin). 我写了一个自动完成列表的存储过程(其中我用crossJoin绑定了两个不同表中的两个列)。

My problem is : 我的问题是:

When I select a Value from Autocomplete TextBox and CLick on Search Button, MY Select Query Behind the Button is not Fetching any Value. 当我从“自动完成文本框”中选择一个值并单击“搜索”按钮时,该按钮后面的“我的选择查询”未获取任何值。

MY Code is: 我的代码是:

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();

Can anyone help me on this 谁可以帮我这个事

That isn't a stored procedure but just a sql command. 那不是存储过程,而只是一个sql命令。

SqlCommand cmd = new SqlCommand("Select * from tblCategoryDetails where CategoryName LIKE '"+ TextSearch.Text"' ", Con); SqlCommand cmd =新的SqlCommand(“从tblCategoryDe​​tails中选择*,其中CategoryName类似于'” + TextSearch.Text“”“,Con);

Try to put wild card % within the SqlCommand 尝试将通配符%放入SqlCommand中

("Select * from tblCategoryDetails where CategoryName LIKE '%"+ TextSearch.Text + "%' ", Con); (“从tblCategoryDe​​tails中选择*,其中CategoryName喜欢'%” + TextSearch.Text +“%”“,Con);

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

In your Markup Code : Inside AutoCompleteExtender : ServiceMEthod should be same as your webmethod class 在您的标记代码中:Inside 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM