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..
 

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

Top