Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Link Enter key with buttons?

I stumbled across a little problem with an ASP.NET Web Application.

I got a couple buttons on my page that I want to access by pressing my Enter key (Depending on which TextBox is focused).

Googled helped me, I thought, but no.

This is what I found:

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

Source

This does not seem to work, it still presses another button that I do not want to be pressed at this moment.

Any suggestions?

like image 750
James Avatar asked Jun 14 '12 17:06

James


2 Answers

It looks like you're using Web Forms.

You can wrap what you're doing inside of a Panel and set the DefaultButton property inside of the panel.

<asp:Panel ID="LoginPanel" runat="server" DefaultButton="btLogin">
  <asp:TextBox ID="txtUser" runat="server" />
  <asp:TextBox ID="txtPass" runat="server" />
  <asp:Button ID="btLogin" runat="server">Login</asp:Button>
</asp:Panel>

When the user has entered data in either txtUser and txtPass and then hit the Enter key, they will trigger the button btLogin.

like image 193
David East Avatar answered Sep 20 '22 06:09

David East


Don't try and use the JavaScript - put your form inside an <asp:Panel> and set the DefaultButton property to be the id of your button.

like image 45
Richard Avatar answered Sep 23 '22 06:09

Richard