How do I associate the Enter key with a button on an aspx page?
Asked Answered
A

5

5

I have an asp.net ascx control file and I have put the control on an aspx page. The aspx page has a button in which when I press enter on the keyboard, I want it to fire the event handler for the button. Is there a way to set this?

I am using a master page with a button already on it, so now when I press the enter key, the event handler for that button fires.

Ahders answered 8/12, 2008 at 19:52 Comment(0)
F
7

DefaultButton also works in panels, so you can set a default button in each panel and have the Enter key click the button in whichever panel currently has focus.

Fourwheeler answered 8/12, 2008 at 20:9 Comment(1)
I've used this in numerous projects and it works great! It also allows you to have multiple "submit" buttons on your page and wrap them in the appropriate context panels to have the proper action occur when enter is pressed.Anarch
D
2

This is subtle in IE. You'll need to add a textbox to your control, set the display to none, and visibility to hidden which shouldn't affect the appearance of your control since this textbox will be hidden, the enter should work correctly, this is an IE issue

Edit: Ignore this if you use .NET 2.0 which introduced defaultButton

Duplication answered 8/12, 2008 at 19:55 Comment(0)
D
1

Try this:

<form id="form1" runat="server" defaultbutton="myButton">
Defy answered 8/12, 2008 at 19:55 Comment(0)
T
1

Page.Form.DefaultButton - msdn link here

Treasatreason answered 8/12, 2008 at 19:57 Comment(0)
S
0

Just a small note: You cannot use the 'defaultButton' attribute if your button is a LinkButton, it doesn't work for that, taken from here: http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx

Sandstrom answered 14/4, 2009 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.