Asp.net Enter Key to submit form

Asp.net Enter Key to submit form

21 Jul 2025
Intermediate
16K Views
4 min read
Learn with an interactive course and practical hands-on labs

Free ASP.NET Core Online Course with Certificate - Start Now

Asp.net Enter Key to submit form: An Overview

Asp.net submit form on enter key, but to achieve that you need to follow some set of procedure. In this Tutorial, we'll explain you how to do that. You can try our ASP.NET Certification Training to learn more about other ASP.NET functionalities.

Read More: Top 50 ASP.NET Core Interview Questions and Answers for 2025

Introduction

Suppose, 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. Let's see how we can do that:

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>

Read More: How can you become an ASP.NET developer

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

  1. We specify the defaultbutton property at the Form level in the form tag when there is only one Submit Button for post back.

  2. We specify the defaultbutton property at the Panel level in the Panel tag when there are multiple Submit Button for post back.

Summary

In this article, I try to explain the default submit behavior of form and panel. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article. To learn more about core concepts of ASP.NET, consider enrolling in our ASP.NET Training.

FAQs

To submit a form in asp.net, you can use <form> tag with <action> attribute. Add the <input  type="submit"> to trigger the submission. Handle the form submission logic within the event handler like 'OnClick' or 'OnSubmit' in server-side of code.

To use  enter key to submit a form, you can add an 'onSubmit' attribute to your <form> tag and specify it with a JavaScript function that will handle form submission.

To turn off enter key in form, you can add an event listener to the form submission event like this:
document.getElementById("yourFormId").addEventListener("submit", function(event) {
  event.preventDefault();
});

To handle form submission errors when using Enter key submission, you should:
  • ensure that the form submission event is intercepted.
  • make sure to catch errors by validating form inputs on submission.
  • always display error messages for clear user feedback.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure Developer Certification Training
13 Sep
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Microservices Certification Training
14 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI Engineer Certification Training
14 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
14 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
.NET Solution Architect Certification Training
14 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this