Hit Enter key to Submit a Form or fire a Default Button Click Event in Asp.Net
It will be better and user friendly if we can submit the input form when the user is pressing the enter button after typing his input. This will provide a better experience to the users in huge input forms where one can submit the form without clicking the submit button explicitly.
In ASP.Net 1.x days, we need can do this by writting a custom javascript code which can identify the enter key press and submit the form.
From ASP.Net 2.0, <form> tag and Panel control has a property called defaultbutton. This enables the user to submit the form when the user hits enter key from the form.
Refer below,
<form id="form1" runat="server" defaultbutton="btnSave">
The above code will fire the btnSave click event on hitting enter key. And,
<asp:Panel ID="Panel1" DefaultButton="btnSave" runat="server"></asp:Panel>
Now, hitting enter key from inside the panel(Panel1) control will fire the click event of the button(btnSave).
Happy coding!!
|