【发布时间】:2012-09-23 14:15:59
【问题描述】:
我想对我的网站进行即时搜索(按下键进行搜索),但我真的不知道有什么好的方法。到目前为止,我一直在搜索“输入”键。
我有这个代码:
<div id="mainsearch" class="search">
<input class="text-input" value="SEARCH" style="color:#aaa" type="text" id="searchbox" onkeydown="if (event.keyCode == 13){document.getElementById('searchLink').click();return false;}" maxlength="50" runat="server" />
<asp:LinkButton ID="searchLink" runat="server" onclick="searchLink_Click">Search</asp:LinkButton>
<div id="results" visible="false" class="ac_results" style="position: absolute; width: 298px;" runat="server">
<asp:Literal ID="litActiveSearch" runat="server"></asp:Literal>
</div>
</div>
在我的代码后面:
protected void searchLink_Click(object sender, EventArgs e)
{
results.Visible = true;
litActiveSearch.Text = SearchResults(searchbox.Value);
}
private string SearchResults(string searchString = "")
{
SqlCommand projectCom = new SqlCommand();
....
countCom.CommandText = "SELECT count(ID_PROJECT) FROM PROJECT WHERE TITLE LIKE '%" + searchString + "%' AND DELETE_BIT = 'False' ";
countCom.CommandType = CommandType.Text;
//int projectRowCount=0,actorRowCount=0,mediaRowCount = 0;
int RowCount = 0;
RowCount = Convert.ToInt32(countCom.ExecuteScalar());
/*str += "<div class=\"ac_results\" style=\"position: absolute; width: 251px; top: 110.4444465637207px;" +
"left: 1010.8776870117188px;\">";*/
str += "<ul>";
str += " <li class=\"ac_even ac_over\"><a href=\" ../search/search.aspx?q=" + searchString + " \" class=\"startsearch\">St<strong>a</strong>rt <strong>" +
"a</strong> full se<strong>a</strong>rch ></a>" +
"</li>";
str += " <li class=\"ac_odd\">" +
"<span class=\"category\">" +
"Projects<a class=\"more\" href=\" ../search/searchProjects.aspx?q=" + searchString + " \" >" + RowCount.ToString() + " results ></a>" +
"</span>" +
"</li>";
//************ Now show the results ************//
projectCom.CommandText = "SELECT TOP 3 ID_PROJECT,TITLE,COUNTRY FROM PROJECT WHERE TITLE LIKE '%" + searchString + "%' AND DELETE_BIT = 'False'";
....
}
在SearchResults() 方法上,我运行我的查询,该查询与我的数据库建立连接,以便在屏幕上获得结果。
这是我第一次做网站,所以我真的什么都不知道。
【问题讨论】:
-
你为什么不简单地使用
TextChanged事件?
标签: c# javascript asp.net ajax