[英]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.