ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Disable Enter key in TextBox to avoid postback in ASP.Net
Author Name: Mudassar Khan Published Date: May 29, 2009
Filed Under :
ASP.Net
 |
JavaScript
Views: 10480

This is a default property that when one presses Enter key in the textbox the form gets submitted in other words there is a postback. So many people asked on forums

1. How to disable enter key in textbox?

2. How to prevent postback or form submission when enter key is pressed in textbox in ASP.Net?

 

So here is the neat and simple trick that does it. And the textbox below is the one that does not do a postback when enter key is pressed inside it

<asp:TextBox ID="TextBox1" runat="server"

   onkeydown = "return (event.keyCode!=13);" >

</asp:TextBox>

 

As you will notice above I wrote a simple condition on onkeydown event.

(event.keyCode!=13);

When enter key is pressed the above condition is false hence false is returned this the postback is disabled.

Note that Visual Studio will give a warning when you add onkeydown event and underline it with green.

It is just a warning and one can ignore that since Visual Studio searched for Server Side events and onkeydown is not in the list. Hence still it will work

But for those who do not want that warning can add this event from code behind

 

C#

TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);");

 

VB.Net

TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

 

The above line will add the onkeydown event to the textbox. Make sure you add these lines to page load.


In order to disable the enter key form submission on all controls simply add it to the body tag in the following way

<body onkeydown = "return (event.keyCode!=13)">


In this way there is no need to add this event to all controls in the page.


The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari 

* All browser logos displayed above are property of their respective owners.


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Pradeep said:
Great post. Something very useful. Thanks for posting.
January 18, 2010  

Theertha Kumar said:
Hibr br Superb....br br Thanks alot..... this solved my postback problem.br
May 25, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code