Simulate button click on Return key press

D

Dan

Consider a simple form with a textbox and button.

Is there a way to handle the common situation where a user presses "return"
rather than clicks on the button.

I can already detect the "return" key press using the textbox's onkeypress
event and now what I'm looking for is something like:

onkeypress="if 'return' key pressed, then button.click=True"

Any ideas?
 
D

Dan

Maybe I should try searching for Enter key first....



You can intercept the client side enter keypress event of the text box and
then click do what you want using javascript code.
Here's a good example:
http://www.kamp-hansen.dk/pages/showdoc.asp?id=28&menuid=21&menuid=18

Or you might try using this free control.
http://www.metabuilders.com/tools/DefaultButtons.aspx

And here's a good article on the subject:
http://www.allasp.net/enterkey.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
 
G

Guest

onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

or in other words just call the buttons click() method.

but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
im sure there's a way to get around this though...
 
D

Dan

Thanks for this.

Adrijan Josic said:
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()"

or in other words just call the buttons click() method..

but I have no idea how to get the buttons id as the .NET framework changes
all ids do something like _ctl1$_ctl4$someButton... naturally you can
viewsource your page and see what id it generates and use that but that
kinda hardcodes and if you change your control tree layout it might stop
working..
 
Joined
Oct 19, 2006
Messages
1
Reaction score
0
With asp.net 2.0 you can enlcose the text box and button inside a panel and then set the DefaultButton property of the panel to the id of the button, when the user presses the enter key the button will be pressed
 
Joined
Jun 6, 2007
Messages
2
Reaction score
0
Here is a quick resolution of your problem

=?Utf-8?B?QWRyaWphbiBKb3NpYw==?= said:
onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

or in other words just call the buttons click() method.

but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
im sure there's a way to get around this though...


You can get the '_ctl1$_ctl4$someButton' button's id as follows:

var buttonClientID = '<%=btnServerButton.UniqueID%>';
var htmlButton = document.getElementById (buttonClientID)

Cheers..
 
Joined
Jun 28, 2007
Messages
1
Reaction score
0
//this code is for Wndows application VS2003 C#
//where txtSearchTerm is a textbox on which you want that Click
//event of btnSearch to get occur.

private void txtSearchTerm_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.Equals( Convert.ToChar(13) ))
{
this.btnSearch_Click(this.btnSearch,null);
}

}
 
Joined
May 18, 2009
Messages
1
Reaction score
0
Panel.DefaultButton

xanoxate said:
With asp.net 2.0 you can enlcose the text box and button inside a panel and then set the DefaultButton property of the panel to the id of the button, when the user presses the enter key the button will be pressed


Ok, this is a very old post, but just wanted to point out that the post by xanoxate is the best one for me. Thanks, I never knew this, and always thought that farting about with Javascript and ClientIDs seemed overkill and something that ASP.NET should have provided out-of-the-box, given how common the scenario is!
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top