Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

ascx

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.

like image 844
Xaisoft Avatar asked Dec 08 '08 19:12

Xaisoft


People also ask

How do you check if Enter key is pressed in asp net?

You can use the Panel. DefaultButton property to automatically click a button when the user presses Enter. If you don't want to show the button, you can set its style="display: none" . This is the best solution IMO.


4 Answers

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.

like image 148
Sisiutl Avatar answered Nov 11 '22 02:11

Sisiutl


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

like image 37
Ta01 Avatar answered Nov 11 '22 01:11

Ta01


Try this:

<form id="form1" runat="server" defaultbutton="myButton">
like image 32
HectorMac Avatar answered Nov 11 '22 01:11

HectorMac


Page.Form.DefaultButton - msdn link here

like image 41
Sergiu Damian Avatar answered Nov 11 '22 02:11

Sergiu Damian