Use enter key to click button?

S

Steve

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
 
J

Jim Cheshire [MSFT]

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
(e-mail address removed)

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


--------------------
 
S

Shawn

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


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
(e-mail address removed)

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


--------------------
 
S

S. Justin Gengo

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 said:
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


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
(e-mail address removed)

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


--------------------
Content-Class: urn:content-classes:message
From: "Steve" <[email protected]>
Sender: "Steve" <[email protected]>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <[email protected]>
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
 
J

Jim Cheshire [MSFT]

There you go. :)

Jim Cheshire, MCSE, MCSD [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

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


--------------------
Reply-To: "S. Justin Gengo" <[email protected]>
From: "S. Justin Gengo" <[email protected]>
References: <[email protected]>
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: <[email protected]>
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 said:
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


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
(e-mail address removed)

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


--------------------
Content-Class: urn:content-classes:message
From: "Steve" <[email protected]>
Sender: "Steve" <[email protected]>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <[email protected]>
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
 
V

Vidar Petursson

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 said:
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


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
(e-mail address removed)

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


--------------------
Content-Class: urn:content-classes:message
From: "Steve" <[email protected]>
Sender: "Steve" <[email protected]>
Subject: Use enter key to click button?
Date: Fri, 21 Nov 2003 02:16:36 -0800
Lines: 12
Message-ID: <[email protected]>
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top