Click here to Skip to main content
15,169,321 members
Articles / Web Development / ASP.NET
Posted 17 Oct 2011

Tagged as


11 bookmarked

How to prevent textbox postback when hitting Enter key in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
17 Oct 2011CPOL
Stop postback in text box when hitting the Enter key, by using jQuery

In ASP.NET, it is default nature that if you press the Enter key on any text box, the page wil post back. To stop this, I had to write a JavaScript function as below:

$(function () {
       $(':text').bind('keydown', function (e) {
        //on keydown for all textboxes
           if ( != "searchtextbox") {
               if (e.keyCode == 13) { //if this is enter key
                   return false;
                  return true;
               return true;

Below is the HTML for the textbox and buttons:

<form id="form1"  runat="server">
        <input id="Button1" type="button" value="Button" " />
        <asp:TextBox ID="txtMessage" runat="server"  Width="438px">

Now if you hit the Enter key in the textbox, then the page will not postback.

For complete code, you can visit:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ram Balak Sharma
Architect Q3 technology
India India
Ram is .Net Architect by profession and passion having 8 year experience. He has extensive experience on Microsoft Development Platform and is also Microsoft Certified Application Developer (MCAD) for Web.
Reach me at
6 Freely avaliable E-Books/

Comments and Discussions

Suggestioncompleting event stack Pin
Dan Randolph16-Oct-15 14:22
MemberDan Randolph16-Oct-15 14:22 
SuggestionUse this in code behind Pin
Nicolò Beltrame3-Mar-13 5:51
professionalNicolò Beltrame3-Mar-13 5:51 
GeneralMy vote of 5 Pin
phicho29-May-12 1:53
Memberphicho29-May-12 1:53 
GeneralReason for my vote of 5 simple solution for a problem we use... Pin
Osvaldo Pontes27-Dec-11 8:27
MemberOsvaldo Pontes27-Dec-11 8:27 
Reason for my vote of 5
simple solution for a problem we used to face with the users
GeneralReason for my vote of 5 nice work Pin
Bilal Ahmed Lilla26-Oct-11 20:54
MemberBilal Ahmed Lilla26-Oct-11 20:54 
GeneralReason for my vote of 5 GR8 i was facing same issue when doi... Pin
ShyamSunderVashista18-Oct-11 20:33
professionalShyamSunderVashista18-Oct-11 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.