Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 17 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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/
Follow on   Twitter

Comments and Discussions

SuggestionUse this in code behind PinmemberNik663-Mar-13 5:51 
GeneralMy vote of 5 Pinmemberphicho29-May-12 1:53 
GeneralReason for my vote of 5 simple solution for a problem we use... PinmemberOsvaldo Pontes27-Dec-11 8:27 
GeneralReason for my vote of 5 nice work PinmemberBilal Ahmed Lilla26-Oct-11 20:54 
GeneralReason for my vote of 5 GR8 i was facing same issue when doi... PinmemberShyamSunderVashista18-Oct-11 20:33 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 17 Oct 2011
Article Copyright 2011 by Ram Balak Sharma
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid