shinshi
11-01-2011, 03:28 PM
hi guys any help would be appreciated..
I got a search textbox that calls out for a javascript function everytime a key is pressed on it.
SOURCE CODE:
<body>
<input type="text" id="txtSearch" runat="server" onkeypress="searchonkeypress()" />
</body>
JAVASCRIPT FUNCTION
<script>
function searchonkeypress() {
var btn = document.getElementById("hiddenbutton");
btn.click();
</script>
this javascript function gets the element of an input type button and force it to get executed. I did that because this button is the one calling my codebehind function that is responsible for populating my datalist.
BUTTON CALLING A CODE BEHIND FUNCTION
<input id="hiddenbutton" runat="server" onserverclick="datalistBind"
type="button" />
So if i type "m" on my search textbox, my codebehind will get executed, and as i pressed more keys same will happen.
This is what my code behind looks like
protected void datalistBind(object sender, EventArgs e)
{
//this is not the actual code, to make it simple ill just give the logic
Sqldatasrouce1.selectcommand = "select * from table where tablefieldname = txtSearch.Text
Sqldatasource1.databind();
}
the problem here is whenever keypressevent is triggered on search textbox(eg i typed in letter "m" which triggers it), it will reload and txtsearchbox would be empty. And on my codebehind im filtering my query by the value of my searchbox. Take note that i dont have any other code that is setting the value of my searchtextbox.. To test this further, i placed a script alert on my page load and i also set there the value of txtboxsearch to "hello".
I typed a letter on my search textbox and the script alert popped up, but my searchbox text is still blank. Second testing, instead of calling the javascript function which calls the codebehind through onkeypress event, i used onchange event. When i changed it, everything worked fine. when i type something on my search box, it doesnt get erased. The problem here is that in onchange event, my codebehind will only be triggered if you leave the focus on your search textbox not on every key pressed. So that means my datalist would only get populated after leaving the focus on my search textbox. Please any help would be appreciated..
im trying to do the facebook invite thing.. In where if you typed in "M" you would see all your friends' name starting with M (mark, mike, marry) and when you typed "Mi" you would only see mike...
thanks alot !
I got a search textbox that calls out for a javascript function everytime a key is pressed on it.
SOURCE CODE:
<body>
<input type="text" id="txtSearch" runat="server" onkeypress="searchonkeypress()" />
</body>
JAVASCRIPT FUNCTION
<script>
function searchonkeypress() {
var btn = document.getElementById("hiddenbutton");
btn.click();
</script>
this javascript function gets the element of an input type button and force it to get executed. I did that because this button is the one calling my codebehind function that is responsible for populating my datalist.
BUTTON CALLING A CODE BEHIND FUNCTION
<input id="hiddenbutton" runat="server" onserverclick="datalistBind"
type="button" />
So if i type "m" on my search textbox, my codebehind will get executed, and as i pressed more keys same will happen.
This is what my code behind looks like
protected void datalistBind(object sender, EventArgs e)
{
//this is not the actual code, to make it simple ill just give the logic
Sqldatasrouce1.selectcommand = "select * from table where tablefieldname = txtSearch.Text
Sqldatasource1.databind();
}
the problem here is whenever keypressevent is triggered on search textbox(eg i typed in letter "m" which triggers it), it will reload and txtsearchbox would be empty. And on my codebehind im filtering my query by the value of my searchbox. Take note that i dont have any other code that is setting the value of my searchtextbox.. To test this further, i placed a script alert on my page load and i also set there the value of txtboxsearch to "hello".
I typed a letter on my search textbox and the script alert popped up, but my searchbox text is still blank. Second testing, instead of calling the javascript function which calls the codebehind through onkeypress event, i used onchange event. When i changed it, everything worked fine. when i type something on my search box, it doesnt get erased. The problem here is that in onchange event, my codebehind will only be triggered if you leave the focus on your search textbox not on every key pressed. So that means my datalist would only get populated after leaving the focus on my search textbox. Please any help would be appreciated..
im trying to do the facebook invite thing.. In where if you typed in "M" you would see all your friends' name starting with M (mark, mike, marry) and when you typed "Mi" you would only see mike...
thanks alot !