By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

Use enter key to click button?

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Steve" <ms**@zuna.co.uk>
Sender: "Steve" <ms**@zuna.co.uk>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <09****************************@phx.gbl>
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


Nov 18 '05 #2

P: n/a
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]" <ja******@online.microsoft.com> wrote in message
news:Jo**************@cpmsftngxa07.phx.gbl...
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
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Steve" <ms**@zuna.co.uk>
Sender: "Steve" <ms**@zuna.co.uk>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <09****************************@phx.gbl>
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

Nov 18 '05 #3

P: n/a
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" <bo********@hotmail.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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]" <ja******@online.microsoft.com> wrote in message
news:Jo**************@cpmsftngxa07.phx.gbl...
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
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Steve" <ms**@zuna.co.uk>
Sender: "Steve" <ms**@zuna.co.uk>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <09****************************@phx.gbl>
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:191798NNTP-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


Nov 18 '05 #4

P: n/a
There you go. :)

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
Reply-To: "S. Justin Gengo" <sj*****@aboutfortunate.com>
From: "S. Justin Gengo" <sj*****@aboutfortunate.com>
References: <09****************************@phx.gbl> <Jo**************@cpmsftngxa07.phx.gbl>
<OF**************@TK2MSFTNGP12.phx.gbl>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: <O3*************@TK2MSFTNGP11.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 63.238.248.99
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
.phx.gbl!TK2MSFTNGP11.phx.gblXref: 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 youto 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, andthen 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" <bo********@hotmail.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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]" <ja******@online.microsoft.com> wrote in message
news:Jo**************@cpmsftngxa07.phx.gbl...
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
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
>Content-Class: urn:content-classes:message
>From: "Steve" <ms**@zuna.co.uk>
>Sender: "Steve" <ms**@zuna.co.uk>
>Subject: Use enter key to click button?
>Date: Fri, 21 Nov 2003 02:16:36 -0800
>Lines: 12
>Message-ID: <09****************************@phx.gbl>
>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.gblmicrosoft.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
>




Nov 18 '05 #5

P: n/a
Hi

<input type="text" onkeydown="if(event.keyCode == 13)
this.form.BUTTONNAME.click()">
Server
myInput.Attributes.Add("onkeydown","if(event.keyCo de == 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" <bo********@hotmail.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
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]" <ja******@online.microsoft.com> wrote in message
news:Jo**************@cpmsftngxa07.phx.gbl...
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
ja******@online.microsoft.com

This post is provided as-is with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Steve" <ms**@zuna.co.uk>
Sender: "Steve" <ms**@zuna.co.uk>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <09****************************@phx.gbl>
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:191798NNTP-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


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.