I want to do an instant search(on key down make the search) for my website and I don't really know any good way to do it. Until now I did my search on 'enter' key.
I have this code:
<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>
and on my code behind:
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'";
....
}
on SearchResults()
method I run my query which makes a connection with my database in order to get the results on the screen.
It is my first time doing a website, so I don't really know anything.