Click here to Skip to main content
15,868,164 members
Articles / Web Development / ASP.NET
Article

Solving out ASP.NET enter key problem

Rate me:
Please Sign up or sign in to vote.
2.95/5 (17 votes)
6 Mar 20073 min read 161K   1.4K   21   16
This article explains about how to solve asp.net enter key problem

Introduction

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 asp.net 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"/>

</asp:panel>

</form>

License

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


Written By
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.

Comments and Discussions

 
GeneralMy vote of 3 Pin
naveen bangwal15-Oct-12 18:08
naveen bangwal15-Oct-12 18:08 
General[My vote of 2] We have better ways.... Pin
Shivprasad koirala17-Apr-11 8:38
Shivprasad koirala17-Apr-11 8:38 
GeneralI prefer the following JavaScript for checking if enter has been presseed... Pin
vodzurk31-Oct-08 1:06
vodzurk31-Oct-08 1:06 
GeneralBug - simple to fix though Pin
ginji5-May-08 16:10
ginji5-May-08 16:10 
GeneralBug with password field in firefox Pin
tuananhpt14-Mar-08 5:37
tuananhpt14-Mar-08 5:37 
GeneralRe: Bug with password field in firefox Pin
N a v a n e e t h17-Mar-08 9:03
N a v a n e e t h17-Mar-08 9:03 
GeneralHallelujah Pin
pepsimanlime4-Sep-07 5:34
pepsimanlime4-Sep-07 5:34 
GeneralRe: Hallelujah Pin
N a v a n e e t h4-Sep-07 18:22
N a v a n e e t h4-Sep-07 18:22 
Generalerror: help! Pin
apestana16-Apr-07 7:22
apestana16-Apr-07 7:22 
GeneralRe: error: help! Pin
N a v a n e e t h19-Apr-07 1:30
N a v a n e e t h19-Apr-07 1:30 
GeneralEnter should be independent Pin
Jan Seda6-Mar-07 23:00
professionalJan Seda6-Mar-07 23:00 
GeneralRe: Enter should be independent Pin
N a v a n e e t h6-Mar-07 23:19
N a v a n e e t h6-Mar-07 23:19 
GeneralRe: Enter should be independent Pin
Jan Seda7-Mar-07 1:02
professionalJan Seda7-Mar-07 1:02 
GeneralRe: Enter should be independent Pin
N a v a n e e t h7-Mar-07 18:20
N a v a n e e t h7-Mar-07 18:20 
GeneralRe: Enter should be independent Pin
Jan Seda7-Mar-07 18:54
professionalJan Seda7-Mar-07 18:54 
And which links would you be interested to? About multiple webforms/page? Microsoft will not provide you with anything. There is nothing provided by Microsoft except Live.com Wink | ;) Just checkout their concept, they are using multiple forms/page and it seems they have reimplemented WebForm/Page as we do. Checkout our code on Dotnetjob.com and you'll see similar approach as Live.com uses. I think ASP.NET team did a VERY bad job when not supporting this feature + GET which are absolutelly essential and that is why you can see many nasty JS non-accessible pages in default asp.net environment Wink | ;)
However you can use our PHP compiler Phalanger too (www.php-compiler.net) which will compile PHP into .NET but you don't have to be limited by ASP.NET obstacles but reuse the power of .net libraries + power of complete control over generated code with PHP.

Jan Seda
Security MVP
www.skilldrive.com, www.dotnetjob.com

GeneralRe: Enter should be independent Pin
TimWallace14-Mar-08 5:18
TimWallace14-Mar-08 5: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.