19

I have a text box and many buttons. When user is in some text box and presses the "enter" key then specific button click event is raised. I read on the internet that there are some problems with the "enter" key and I tried few solutions but still it always enters that button event (that button is the first button in the page).

I tried creating a button which does nothing and writing this in the page_load:

idTextBoxFA.Attributes.Add("onkeydown","if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementBtId('" + noEnterButton.UniqueID + "').click();return false;}} else {return true}; ");

All my page controls are in a form and I tried giving the form "defaultButton" property to that button I created. That didnt work either.

Any idea why this isn't working and what am I doing wrong?

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
Greg Oks
  • 2,700
  • 4
  • 35
  • 41
  • Possible duplicate of [how to set a default 'enter' on a certain button](http://stackoverflow.com/questions/7638119/how-to-set-a-default-enter-on-a-certain-button) – Tot Zam Dec 30 '15 at 01:48
  • The two questions are so similar that I wasn't sure which one to flag. StackOverflow's trend is to pick the clearer anf better formatted question, not the one that was posted first. In this case however, even the phrasing was similar enough, so I went with the more popular question and the one that had more votes. Sorry. – Tot Zam Dec 30 '15 at 12:42

3 Answers3

39

The standard way in ASP.NET to control which submit button is activated when ENTER is pressed is to wrap the controls and buttons in an asp:Panel with the DefaultButton property set to the correct button.

If I'm reading your question properly, you just want one specific button to be activated when ENTER is pressed, so wrap everything on your page in a single asp:Panel.

<asp:Panel id="pnlDefaultButton" runat="server" DefaultButton="btnOK">
    <!-- All controls here including: -->
    <asp:Button id="btnOK" runat="server" Text="OK" />
</asp:Panel>
Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
  • What about asp:Content? I use master pages and have no panel nor form on my page. – Marcel Sep 14 '12 at 06:56
  • @Marcel - I'm not certain what you mean. Using master pages doesn't change much, unless the button is in one content section and the other parts of the page somewhere else. In that situation, you could rearrange your content, or look into a pure JavaScript solution. – Jason Berkan Sep 14 '12 at 16:28
  • @Jason Berkan: I now did like described here: http://stackoverflow.com/a/3313285/79485 – Marcel Sep 15 '12 at 18:41
8

No panel is needed. Just use the following:

UseSubmitBehavior="false"
Paweł Bejger
  • 6,176
  • 21
  • 26
Fred
  • 81
  • 1
  • 1
0

Please use

idTextBoxFA.Attributes.Add("onkeypress", "javascript:var evnt = window.event";
if(evnt.keyCode==13)
{
document.getElementById('" + noEnterButton.ClientID + "').click();
}
else{};");
KARTHIK BHAT
  • 1,410
  • 13
  • 23