Use enter key to click button?

Discussion in 'ASP .Net' started by Steve, Nov 21, 2003.

  1. Steve

    Steve Guest

    Hi,

    Is it possible to make hitting the enter key in an ASP
    textbox run the code behind an ASP button on a form?

    I have a search page which users tend to type in the
    query then just hit enter rather than actually clicking
    the search button, but this does not run the search code
    behind the button.

    Thanks for any advice,
    Steve
     
    Steve, Nov 21, 2003
    #1
    1. Advertising

  2. Steve,

    You can do this with client-side code. For example:

    function clickButton() {
    if (event.keyCode == 13) {
    document.form.button1.click();
    return false;
    }
    }


    Hope that helps.

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.


    --------------------
    >Content-Class: urn:content-classes:message
    >From: "Steve" <>
    >Sender: "Steve" <>
    >Subject: Use enter key to click button?
    >Date: Fri, 21 Nov 2003 02:16:36 -0800
    >Lines: 12
    >Message-ID: <09ff01c3b018$8bdbd710$>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >Thread-Index: AcOwGIvb9mDTwp3NRt2rOdR4idmNxA==
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >Path: cpmsftngxa07.phx.gbl
    >Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191798
    >NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    >Hi,
    >
    >Is it possible to make hitting the enter key in an ASP
    >textbox run the code behind an ASP button on a form?
    >
    >I have a search page which users tend to type in the
    >query then just hit enter rather than actually clicking
    >the search button, but this does not run the search code
    >behind the button.
    >
    >Thanks for any advice,
    >Steve
    >
     
    Jim Cheshire [MSFT], Nov 21, 2003
    #2
    1. Advertising

  3. Steve

    Shawn Guest

    Shouldn't hitting enter in the textbox fire the button's click event without
    adding client-side code? It doesn't happen if you have only one textbox,
    but if I add a second textbox then hitting enter will fire the button's
    click event. Isn't that kind of strange?

    BTW: How is the clickButton() script called? From the textbox?

    Thanks,
    Shawn


    "Jim Cheshire [MSFT]" <> wrote in message
    news:...
    Steve,

    You can do this with client-side code. For example:

    function clickButton() {
    if (event.keyCode == 13) {
    document.form.button1.click();
    return false;
    }
    }


    Hope that helps.

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.


    --------------------
    >Content-Class: urn:content-classes:message
    >From: "Steve" <>
    >Sender: "Steve" <>
    >Subject: Use enter key to click button?
    >Date: Fri, 21 Nov 2003 02:16:36 -0800
    >Lines: 12
    >Message-ID: <09ff01c3b018$8bdbd710$>
    >MIME-Version: 1.0
    >Content-Type: text/plain;
    > charset="iso-8859-1"
    >Content-Transfer-Encoding: 7bit
    >X-Newsreader: Microsoft CDO for Windows 2000
    >Thread-Index: AcOwGIvb9mDTwp3NRt2rOdR4idmNxA==
    >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >Path: cpmsftngxa07.phx.gbl
    >Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191798
    >NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    >Hi,
    >
    >Is it possible to make hitting the enter key in an ASP
    >textbox run the code behind an ASP button on a form?
    >
    >I have a search page which users tend to type in the
    >query then just hit enter rather than actually clicking
    >the search button, but this does not run the search code
    >behind the button.
    >
    >Thanks for any advice,
    >Steve
    >
     
    Shawn, Nov 21, 2003
    #3
  4. Shawn (and Steve who started this thread),

    If you'd like a quick way to define which button is clicked when the enter
    key is hit while inside a text box I've created a javascript that allows you
    to specify this.

    If you have two or more text boxes on a page you may specify that they all
    trigger the same button or each click their own separate button.

    The script I use is very similar to the one Jim showed as an example except
    that it's cross browser compatible.

    Just go to my site, www.aboutfortunate.com, click the code library link, and
    then click the Javascript button in the menu on the left. The component is
    free and has many useful javascripts in it. I also created a help file for
    it and the entire project is downloadable so you can change any of the code
    etc.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche


    "Shawn" <> wrote in message
    news:...
    > Shouldn't hitting enter in the textbox fire the button's click event

    without
    > adding client-side code? It doesn't happen if you have only one textbox,
    > but if I add a second textbox then hitting enter will fire the button's
    > click event. Isn't that kind of strange?
    >
    > BTW: How is the clickButton() script called? From the textbox?
    >
    > Thanks,
    > Shawn
    >
    >
    > "Jim Cheshire [MSFT]" <> wrote in message
    > news:...
    > Steve,
    >
    > You can do this with client-side code. For example:
    >
    > function clickButton() {
    > if (event.keyCode == 13) {
    > document.form.button1.click();
    > return false;
    > }
    > }
    >
    >
    > Hope that helps.
    >
    > Jim Cheshire, MCSE, MCSD [MSFT]
    > Developer Support
    > ASP.NET
    >
    >
    > This post is provided as-is with no warranties and confers no rights.
    >
    >
    > --------------------
    > >Content-Class: urn:content-classes:message
    > >From: "Steve" <>
    > >Sender: "Steve" <>
    > >Subject: Use enter key to click button?
    > >Date: Fri, 21 Nov 2003 02:16:36 -0800
    > >Lines: 12
    > >Message-ID: <09ff01c3b018$8bdbd710$>
    > >MIME-Version: 1.0
    > >Content-Type: text/plain;
    > > charset="iso-8859-1"
    > >Content-Transfer-Encoding: 7bit
    > >X-Newsreader: Microsoft CDO for Windows 2000
    > >Thread-Index: AcOwGIvb9mDTwp3NRt2rOdR4idmNxA==
    > >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet
    > >Path: cpmsftngxa07.phx.gbl
    > >Xref: cpmsftngxa07.phx.gbl

    microsoft.public.dotnet.framework.aspnet:191798
    > >NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
    > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > >
    > >Hi,
    > >
    > >Is it possible to make hitting the enter key in an ASP
    > >textbox run the code behind an ASP button on a form?
    > >
    > >I have a search page which users tend to type in the
    > >query then just hit enter rather than actually clicking
    > >the search button, but this does not run the search code
    > >behind the button.
    > >
    > >Thanks for any advice,
    > >Steve
    > >

    >
    >
     
    S. Justin Gengo, Nov 21, 2003
    #4
  5. There you go. :)

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.


    --------------------
    >Reply-To: "S. Justin Gengo" <>
    >From: "S. Justin Gengo" <>
    >References: <09ff01c3b018$8bdbd710$>

    <>
    <>
    >Subject: Re: Use enter key to click button?
    >Date: Fri, 21 Nov 2003 09:13:26 -0600
    >Lines: 108
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
    >Message-ID: <>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >NNTP-Posting-Host: 63.238.248.99
    >Path:

    cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP08
    .phx.gbl!TK2MSFTNGP11.phx.gbl
    >Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:191869
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >
    >Shawn (and Steve who started this thread),
    >
    >If you'd like a quick way to define which button is clicked when the enter
    >key is hit while inside a text box I've created a javascript that allows

    you
    >to specify this.
    >
    >If you have two or more text boxes on a page you may specify that they all
    >trigger the same button or each click their own separate button.
    >
    >The script I use is very similar to the one Jim showed as an example except
    >that it's cross browser compatible.
    >
    >Just go to my site, www.aboutfortunate.com, click the code library link,

    and
    >then click the Javascript button in the menu on the left. The component is
    >free and has many useful javascripts in it. I also created a help file for
    >it and the entire project is downloadable so you can change any of the code
    >etc.
    >
    >--
    >Sincerely,
    >
    >S. Justin Gengo, MCP
    >Web Developer / Programmer
    >
    >Free code library at:
    >www.aboutfortunate.com
    >
    >"Out of chaos comes order."
    > Nietzche
    >
    >
    >"Shawn" <> wrote in message
    >news:...
    >> Shouldn't hitting enter in the textbox fire the button's click event

    >without
    >> adding client-side code? It doesn't happen if you have only one textbox,
    >> but if I add a second textbox then hitting enter will fire the button's
    >> click event. Isn't that kind of strange?
    >>
    >> BTW: How is the clickButton() script called? From the textbox?
    >>
    >> Thanks,
    >> Shawn
    >>
    >>
    >> "Jim Cheshire [MSFT]" <> wrote in message
    >> news:...
    >> Steve,
    >>
    >> You can do this with client-side code. For example:
    >>
    >> function clickButton() {
    >> if (event.keyCode == 13) {
    >> document.form.button1.click();
    >> return false;
    >> }
    >> }
    >>
    >>
    >> Hope that helps.
    >>
    >> Jim Cheshire, MCSE, MCSD [MSFT]
    >> Developer Support
    >> ASP.NET
    >>
    >>
    >> This post is provided as-is with no warranties and confers no rights.
    >>
    >>
    >> --------------------
    >> >Content-Class: urn:content-classes:message
    >> >From: "Steve" <>
    >> >Sender: "Steve" <>
    >> >Subject: Use enter key to click button?
    >> >Date: Fri, 21 Nov 2003 02:16:36 -0800
    >> >Lines: 12
    >> >Message-ID: <09ff01c3b018$8bdbd710$>
    >> >MIME-Version: 1.0
    >> >Content-Type: text/plain;
    >> > charset="iso-8859-1"
    >> >Content-Transfer-Encoding: 7bit
    >> >X-Newsreader: Microsoft CDO for Windows 2000
    >> >Thread-Index: AcOwGIvb9mDTwp3NRt2rOdR4idmNxA==
    >> >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    >> >Newsgroups: microsoft.public.dotnet.framework.aspnet
    >> >Path: cpmsftngxa07.phx.gbl
    >> >Xref: cpmsftngxa07.phx.gbl

    >microsoft.public.dotnet.framework.aspnet:191798
    >> >NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
    >> >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    >> >
    >> >Hi,
    >> >
    >> >Is it possible to make hitting the enter key in an ASP
    >> >textbox run the code behind an ASP button on a form?
    >> >
    >> >I have a search page which users tend to type in the
    >> >query then just hit enter rather than actually clicking
    >> >the search button, but this does not run the search code
    >> >behind the button.
    >> >
    >> >Thanks for any advice,
    >> >Steve
    >> >

    >>
    >>

    >
    >
    >
     
    Jim Cheshire [MSFT], Nov 21, 2003
    #5
  6. Hi

    <input type="text" onkeydown="if(event.keyCode == 13)
    this.form.BUTTONNAME.click()">
    Server
    myInput.Attributes.Add("onkeydown","if(event.keyCode == 13)
    this.form.BUTTONNAME.click()");
    Where BUTTONNAME is the btn to click

    IE only

    --
    Best Regards
    Vidar Petursson
    ==============================
    Microsoft Scripting MVP
    http://www.microsoft.com/technet/scriptcenter
    ==============================
    "Shawn" <> wrote in message
    news:...
    > Shouldn't hitting enter in the textbox fire the button's click event

    without
    > adding client-side code? It doesn't happen if you have only one textbox,
    > but if I add a second textbox then hitting enter will fire the button's
    > click event. Isn't that kind of strange?
    >
    > BTW: How is the clickButton() script called? From the textbox?
    >
    > Thanks,
    > Shawn
    >
    >
    > "Jim Cheshire [MSFT]" <> wrote in message
    > news:...
    > Steve,
    >
    > You can do this with client-side code. For example:
    >
    > function clickButton() {
    > if (event.keyCode == 13) {
    > document.form.button1.click();
    > return false;
    > }
    > }
    >
    >
    > Hope that helps.
    >
    > Jim Cheshire, MCSE, MCSD [MSFT]
    > Developer Support
    > ASP.NET
    >
    >
    > This post is provided as-is with no warranties and confers no rights.
    >
    >
    > --------------------
    > >Content-Class: urn:content-classes:message
    > >From: "Steve" <>
    > >Sender: "Steve" <>
    > >Subject: Use enter key to click button?
    > >Date: Fri, 21 Nov 2003 02:16:36 -0800
    > >Lines: 12
    > >Message-ID: <09ff01c3b018$8bdbd710$>
    > >MIME-Version: 1.0
    > >Content-Type: text/plain;
    > > charset="iso-8859-1"
    > >Content-Transfer-Encoding: 7bit
    > >X-Newsreader: Microsoft CDO for Windows 2000
    > >Thread-Index: AcOwGIvb9mDTwp3NRt2rOdR4idmNxA==
    > >X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet
    > >Path: cpmsftngxa07.phx.gbl
    > >Xref: cpmsftngxa07.phx.gbl

    microsoft.public.dotnet.framework.aspnet:191798
    > >NNTP-Posting-Host: tk2msftngxa09.phx.gbl 10.40.1.161
    > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    > >
    > >Hi,
    > >
    > >Is it possible to make hitting the enter key in an ASP
    > >textbox run the code behind an ASP button on a form?
    > >
    > >I have a search page which users tend to type in the
    > >query then just hit enter rather than actually clicking
    > >the search button, but this does not run the search code
    > >behind the button.
    > >
    > >Thanks for any advice,
    > >Steve
    > >

    >
    >
     
    Vidar Petursson, Nov 22, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bsm
    Replies:
    0
    Views:
    1,341
  2. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    429
    Anders Matthiessen
    Jan 12, 2005
  3. M P
    Replies:
    1
    Views:
    524
  4. Matt
    Replies:
    1
    Views:
    545
    Michael Winter
    Mar 6, 2004
  5. HopfZ
    Replies:
    0
    Views:
    614
    HopfZ
    Aug 28, 2006
Loading...

Share This Page