Results 1 to 8 of 8

Thread: Perform action when user presses <Enter> on keyboard.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Perform action when user presses <Enter> on keyboard.

    I have this simple search form and would like for the search button action to act when the user presses the <enter> key on the keyboard instead of having to click on the search button.

    Can someone please tell me how to do this. I see there is an AccessKey button to associate a keyboard shortcut but do not know what value to place in there if that is the right solution.

    Any help would be appreciated.

    Thanks.

  2. #2
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: Perform action when user presses <Enter> on keyboard.

    prob the best way is to have only 1 button, because once you hit enter the button will br triggered.

  3. #3
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Perform action when user presses <Enter> on keyboard.

    Code:
    <asp:textbox id="txtSearch" onKeyUp="__doSearchReturn()" runat="server"></asp:textbox> <asp:button id="btnSearch" text="Search" runat="server"></asp:button>
    <script language="JavaScript">
    function __doSearchReturn(e)
    {
        if(e.keyCode)
        {//ie
             if(e.keyCode == 13){__doPostBack('btnSearch','');}
         }
         else
        {//n4
             if(e.which == 13){__doPostBack('btnSearch','');}
         }
    }
    </script>
    something like that....
    Magiaus

    If I helped give me some points.

  4. #4
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: Perform action when user presses <Enter> on keyboard.

    Magiaus

    Do you know what the equivalent for e.which is in Netscape 7? It doesn't seem to like it.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Perform action when user presses <Enter> on keyboard.

    Thanks again for answering Magiaus, I entered it in just like you have it there, but it still doesnt seem to work. Nothing happens.

    Am I missing something?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Perform action when user presses <Enter> on keyboard.

    Oh, and 'OnKeyUP' doesnt seem to be recognized, it tells me: Could not find any attribute 'onKeyUp' of element 'Textbox'.

    And one other thing is that the button is an <asp:imagebutton> if that makes any difference.

    And to Strider:
    I do only have one button, is there a property I am not setting?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    Re: Perform action when user presses <Enter> on keyboard.

    Okay, i found the solution, by searching the internet.
    Simply put this line of code in your OnLoad event in your code-behind:

    Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")

    and voila it works.

    Thanks again everyone for helping me out.

    Here is the page i found the solution on:
    http://www.developer.com/net/asp/article.php/1594521

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: Perform action when user presses <Enter> on keyboard.

    Don't know about Navigator 7 I just checked in my ye old JavaScript book from like 5 years ago lol.
    Magiaus

    If I helped give me some points.

Posting Permissions

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



Click Here to Expand Forum to Full Width