[英]Search Based on AutoComplete Extender Value in asp.net
我有一个带有AutoCompelte Extender的textBox,自动填充文本来自数据库。
我写了一个自动完成列表的存储过程(其中我用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命令。
SqlCommand cmd =新的SqlCommand(“从tblCategoryDetails中选择*,其中CategoryName类似于'” + TextSearch.Text“”“,Con);
尝试将通配符%放入SqlCommand中
(“从tblCategoryDetails中选择*,其中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;
}
在您的标记代码中: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.