Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with onkeypress event calling code behind function

    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 !

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,928
    Thanks
    46
    Thanked 203 Times in 202 Posts
    first, remove the runat="server" for your input... if the input's only function is to act as a search window that you are redirecting then remove the runat, pass the value (this link should help for ideas of how to do so)... if you are going to make javascript mandatory for this I think scripting through a set of data would be more efficient then hopping back and forth to and from the server... pull all your data down on the first hop and then filter through it via script

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    shinshi (11-02-2011)

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hi alykins! thank you for your response...
    i removed the runat server on my search input textbox to no avail.. whenever a key is pressed, my search textbox is still getting erased. the same happens even if i dont put any code on my codebehind function... I tried to follow the link you have given me but i think its not working..

    Im gonna try to do the other suggestion u gave me.. But im still gonna try to make this work coz this is bugging me.. how come on onchange event its working, and on keypressevent its not.. I mean, they are both events, the only difference i see on them is that how they are getting triggered.

    thanks again alykins

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,928
    Thanks
    46
    Thanked 203 Times in 202 Posts
    I've never used javascript to fire a btn click.... does btn.click(); fire the onserverclick fxn? I would think it should just be OnClick="datalistBind()" ... anyways... does the binding actually occur and do you get data back? also what onchange event? are you talking about a listbox somewhere that is not shown in above code? If so it could verywell have autopostback set to false, therefore onchange events in the listbox would not fire a postback...

    let's think about what is happening though... if your button click is triggering a post back (which I think it is) then every time you type something in the box, you now do a postback, and that box now doesnt have that text in it anymore... you need to make it not postback.... thinking about this process more, I think you will have to pull all the data and then parse through it....
    hmmm thinking about it* idk how google does it

    anyways those are my thoughts

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •