Thursday, 12 December 2013

Enter Key to submit form in Asp.Net


You want to press/click submit button on Enter key press or you are trying to post the form on Enter key press. In asp.net, to achieve this functionality we need to set "Defaultbutton" property either in Form or in panel.

Form DefaultButton Property

<form id="form1" runat="server" defaultbutton="btnSubmit">
<div>
<asp:TextBox ID="txtUserID" runat="server"/> 
<asp:TextBox ID="txtUserpwd" runat="server"/> 
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
</div>
</form>
Panel DefaultButton Property

<asp:Panel ID="Panel1" runat="server" defaultbutton="btnSubmit">
<div>
<asp:TextBox ID="txtUserID" runat="server"/>
<asp:TextBox ID="txtUserpwd" runat="server"/> 
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
</div>
</asp:Panel >
Note
We specify the defaultbutton property at the Form level in the form tag when there is only one Submit Button for post back.
We specify the defaultbutton property at the Panel level in the Panel tag when there are multiple Submit Button for post back.


No comments:

Post a Comment