Click here to Skip to main content
13,344,016 members (84,649 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 6 Mar 2007

Solving out ASP.NET enter key problem

, 6 Mar 2007
Rate this:
Please Sign up or sign in to vote.
This article explains about how to solve enter key problem


When I start programming with ASP.NET I used to face problems with enter key. Clicking enter key won't make a button click at all times. It will post back the form, but the relevant button you wanted to call won't be called. I read several articles which discuss this problem and tried some of the methods suggested by them. But nothing gave me a stable performance. So I created a library that can set default button for text box control when the enter key is pressed.

Identifying problem

Hitting enter key on ASP.NET pages will produce really different results. It varies on each browser. You can test it by placing one text box and one button on a webform. Write some code in button's onclick event. It could be some thing like

Response.write("You clicked this button");

Try debugging the program. Pressing enter key on the text box will make a postback. But whatever code you written inside button click won't be executed. This will work some time in firefox.

Then I found some ASP.NET developers was saying, "you can achieve this by putting one hidden field". So I placed something like this.

<asp:TextBox runat="server" style="visibility:hidden;display:none;" />

Start debugging again. Now pressing enter key will post form and execute the code for the button's onclick. Looks strange !

But that worked as temporary solution. This method can't be implemented when there is more than one button.

Assume you have two text boxes and two buttons. Hitting enter key from first text box should call first button and second text box should call second button. In this case hidden textbox method fails. It will call one button every time.

To solve this and make it work with all situations we can use javascript.

Using javascript for solution

txtBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+Button1.UniqueID+"').click();return false;}} else {return true}; ");

Adding the above attribute to the textbox will solve the problem. The keycode for enter key is 13. When a key is pressed in textbox it will check the keycode is 13. If it is 13 the relevant button will be clicked. event.keycode will not work with firefox, so we are using event.which.

Porting into DefaultButton class

I ported the same code into a class. Using this you can pass the textbox object and button object. It will set the necessary scripts for making enter key working. There are two static methods in DefaultButton class. One is for normal button and next is for image button.

To set default button for one text box follow these steps

  • Add reference to the library
  • Import System.Web.UI.DefaultButton namespace
  • Use DefaultButton.SetDefaultButton ( control , btButton )

This method will add keydown attribute for the textbox control and calls button when enter key is pressed.

Setting default buttons in ASP,NET 2.0

ASP.NET 2.0 comes with defaultbutton property. You can set default button for form and panels. This will be called when form is posting back using enter key.

<form defaultbutton="btn1" runat="server">

<asp:textbox id="txt1" runat="server"/>

<asp:button id="btn1" text="Button on Form" runat="server"/>

<asp:panel defaultbutton="btn2" runat="server">

<asp:textbox id="txt2" runat="server"/>
<asp:button id="btn2" text="Button on panel" runat="server"/>




This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

N a v a n e e t h
Software Developer ThoughtWorks
India India
Call me Navaneeth Some years ago--never mind how long precisely, I was doing programs with C and C++. When .NET came, I started with C#,ASP.NET and SQL Server.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
naveen bangwal15-Oct-12 19:08
membernaveen bangwal15-Oct-12 19:08 
General[My vote of 2] We have better ways.... Pin
Shivprasad koirala17-Apr-11 9:38
mvpShivprasad koirala17-Apr-11 9:38 
GeneralI prefer the following JavaScript for checking if enter has been presseed... Pin
vodzurk31-Oct-08 2:06
membervodzurk31-Oct-08 2:06 
GeneralBug - simple to fix though Pin
ginji5-May-08 17:10
memberginji5-May-08 17:10 
GeneralBug with password field in firefox Pin
tuananhpt14-Mar-08 6:37
membertuananhpt14-Mar-08 6:37 
GeneralRe: Bug with password field in firefox Pin
N a v a n e e t h17-Mar-08 10:03
memberN a v a n e e t h17-Mar-08 10:03 
GeneralHallelujah Pin
pepsimanlime4-Sep-07 6:34
memberpepsimanlime4-Sep-07 6:34 
GeneralRe: Hallelujah Pin
N a v a n e e t h4-Sep-07 19:22
memberN a v a n e e t h4-Sep-07 19:22 
Generalerror: help! Pin
apestana16-Apr-07 8:22
memberapestana16-Apr-07 8:22 
GeneralRe: error: help! Pin
DooDooo19-Apr-07 2:30
memberDooDooo19-Apr-07 2:30 
GeneralEnter should be independent Pin
Jan Seda7-Mar-07 0:00
memberJan Seda7-Mar-07 0:00 
GeneralRe: Enter should be independent Pin
Navaneethkn7-Mar-07 0:19
memberNavaneethkn7-Mar-07 0:19 
GeneralRe: Enter should be independent Pin
Jan Seda7-Mar-07 2:02
memberJan Seda7-Mar-07 2:02 
GeneralRe: Enter should be independent Pin
Navaneethkn7-Mar-07 19:20
memberNavaneethkn7-Mar-07 19:20 
GeneralRe: Enter should be independent Pin
Jan Seda7-Mar-07 19:54
memberJan Seda7-Mar-07 19:54 
GeneralRe: Enter should be independent Pin
TimWallace14-Mar-08 6:18
memberTimWallace14-Mar-08 6:18 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 7 Mar 2007
Article Copyright 2007 by N a v a n e e t h
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid