ASP.NET Link Enter key with buttons?

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.


Use jQuery for this is much easier. Bind to keypress event.


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.