logo
  • Jobs
  • About Me
  • Contact
  • Home
« MovableType 2.64
MCSD Bound »

Submitting ASP.NET form with ENTER key

Posted June 2nd, 2003 by Matt Berther

For the last few hours, I’ve been trying to figure out why my event handler is not being hooked up when I hit the ENTER key to submit my ASP.NET form.

As it turns out, this is a bug with ASP.NET. If you’re not aware of this, the work around is to insert an invisible *HTML* textbox (an asp:TextBox wont work).

Add this line somewhere on the page:

<input type="text" style="display:none"/>

and all will work as expected.

This entry was posted on Monday, June 2nd, 2003 at 3:31 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Chris Scott
December 15th, 2003

Thank you! This works perfectly.

All The Good Titles Were Taken
December 15th, 2003

why the enter key doesn’t submit an asp.net form

Thanks to Matt Berther for his post regarding why the enter key wasn’t working to submit an ASP.NET form (using …

Anonymous
January 18th, 2004

Submitting ASP.NET form with enter key

This has got to be the wierdest fix I’ve ever seen, but it works. I was completely puzzled as to why my search button would -not- work when I entered data and hit enter. It would just not do anything. Then I read this article on Matt Berther’s blog:For

aaron weiker
September 3rd, 2004

RE: *MORE* ON ASP.NET: How to create a Default

Ilya Karlik
September 15th, 2004

It acutually works fine with asp:TextBox if you make it runat=server and set it’s style=visibility:hidden, but the solution above looks better anyway

nik
November 12th, 2004

Thank you! Thank you! What a bizarre and annoying bug.

shane
December 13th, 2004

i have a web page that has two ascx’s included in a form. when i press the enter key it moves the focus to one of the controls buttons every time. i have tries using javascript with the onsubmit event to transfer focus to the correct button but it seems that the onsubmit event is being fired after some internal javascript in the asp page. i think that is has something to do with the JS that is emitted from the controls with the page loads. I have read many articles regarding submitting forms with the enter key and i havent found a perfect solution yet. it seems that asp.net has some weird behavoirs that may actually be bugs regarding this configuration. If anyone can help i would be very thankful
the page is located at http://www.gso.com/rates.aspx

Jeffrey
December 22nd, 2004

Thank you for the posted solution. I have seen some really complicated and overdone solutons to this issue so this is a welcome fix. FYI - this issue doesnt seem to happen in Firefox! Only IE… strange but true. :-)

Francois
January 26th, 2005

IF you added AutoPostBack=”true” in the asp:textbox would it not send back as soon as the value changes?

Geeger
February 2nd, 2005

I don’t get it, this code did not work for me. I’m still unable to send my form using the Enter key.

AK
February 7th, 2005

Thanks so much for this fix. I’ve been searchin for quite some time for this.

:)

Nate Davis
February 14th, 2005

Why have I not seen this anywhere else? It’s such a simple fix. Thanks for the help.

David
February 23rd, 2005

Ok - this invisible text box works, but can anyone explain WHY?

Tester
March 3rd, 2005

If this gets submitted, then Firefox 1.0 is submitting the form when I hit enter from within the Name textbox.

I don’t want this behavior.

Carson
March 10th, 2005

Thanks a million for this fix. At first it didn’t work, but when i moved it around (and also rearranged my fields) it worked fine. Bill gates sucks.

RiaanS
March 24th, 2005

Submitting ASP.NET form with enter key

This has got to be the wierdest fix I’ve ever seen, but it works. I was completely puzzled as to why…

Riaan's Blog
March 24th, 2005

Submitting ASP.NET form with enter key

Skeptical
April 29th, 2005

Cool Beans!

Kris Kilowatt
May 18th, 2005

GREAT! I came here typing “asp.net” post “enter key” in Google and it was the first result on the page. Looked further and others use javascript etc.
THANKS!

dblum
May 19th, 2005

This is awesome. Thanks so much for the post. It worked like a champ!!!

borabanana
June 6th, 2005

This is a quick fix in certain situations. However, does anyone know if it’s an ASP bug or a IE bug? or whether there’s a msdn entry somewhere? what might work for IE may be a disaster for all the other browsers out there

saimatkong
June 9th, 2005

i have a web page that has two ascx’s included in a form. when i press the enter key it moves the focus to one of the controls buttons every time. i have tries using javascript with the onsubmit event to transfer focus to the correct button but it seems that the onsubmit event is being fired after some internal javascript in the asp page. i think that is has something to do with the JS that is emitted from the controls with the page loads. I have read many articles regarding submitting forms with the enter key and i havent found a perfect solution yet. it seems that asp.net has some weird behavoirs that may actually be bugs regarding this configuration. If anyone can help i would be very thankful
the page is located at http://www.gso.com/rates.aspx

hey i have the same prob here.
but it seems it’s solve in http://www.gso.com/rates.aspx
how can i do tat ?
thanks.

Hitesh
June 21st, 2005

Great….Thanx a lot….

Drew
June 21st, 2005

This freebie fixed the ascx problem for me. http://www.metabuilders.com/Tools/DefaultButtons.aspx

Sam
June 29th, 2005

I have two pages almost exactly the same. One page needed this fix, the other one worked the way it was supposed to without it (both had some asp:textboxes and a asp:button for submitting). Microsoft can’t even have a bug that operates consistently! I put the fix in both anyways; it doesn’t seem like it could possibly cause a problem

Sumit Ranjan
July 15th, 2005

Thank you! It’s really the simplest method according to me.

Teena R. Dadha
July 20th, 2005

It is a great fix.
A lot of client scripts have been suggested on the net, but they didn’t work for me

Xiao Ma
July 28th, 2005

Why do you guys think this is an ASP.NET bug? Simply try the following and you will know it’s an IE bug.

function foo() {
alert(”submit”);
document.forms[0].submit();
}

yuva
January 2nd, 2006

It is neither .net bug or IE bug.
it is not at all a bug.

y don’t u people use __dopostback(btnId,”) on pressing enter in a textbox.
i.e call a javascript fn onkeypressed event of textbox, if its “ENTER”, then u can execute ur desired button event handler by just calling __dopostback(btnId,”) .

jonathan
January 16th, 2006

hey, why the code is not work for me??
i have few text box and a button, i want the enter have same function as button click events

Drew
February 27th, 2006

I know why it should work, but it does not work for me. I have a variety of ASP:IMAGEBUTTON objects, and the first of which is not considered my default. It is, however, my first button on the page. Guess which one fires when i press ‘enter’. You got it! What code do i put in my other (considered default) button? __dopostback(”,”) ???

Thanks for the suggested fix. Glad it works for some.

Juan Romero
April 11th, 2006

This is why this fix works:
http://aspnet.4guysfromrolla.com/articles/060805-1.aspx

Madhuri
April 21st, 2006

Great!Thanks.

Never thought it would work ……………..

for those who didnt work, set the tab index of the submit button to 0 and try. and place this text box just before the submit button

Madhuri
April 21st, 2006

Even this worked great

Add this code to your OnPageLoad event

Page.RegisterHiddenField(”__EVENTTARGET”, “btnSearch”);

where btnSearch is your command button ID/Name.

This offers more flexibility if you have more than one command buttons on your page.

keith@UC
April 21st, 2006

Does anyone know if this works for multiple textboxes and buttons? If so, how would I set that up?

My page is essentially:
[textbox1] [button1]
[textbox2] [button2]

But when I hit enter some text into textbox2 it submits button1.

PS: I tried metabuilders control above but it is kind of a hassle.

Thanks

Juan Romero
May 23rd, 2006

I don’t see why you would need to handle it this way for multiple textboxes and buttons. Keep in mind that this situation happens because there is only one textbox on the page, but if you have more than one then the framework will post the values back and all you need to do is handle each button’s event.

Frank
June 23rd, 2006

You sir, are a life saver. Was driving me nuts because it only happens on pages with one text box, all the rest work fine.

Why is this not documented anywhere as a bug? I would think that this would show up in 1k places?????.

Anyway, this is exactly what i was looking for, thanks again.

Frank

Raj
July 14th, 2006

Thanks, I never thought that this problem will have such a twisting solution

Sam
August 15th, 2006

saimatkong, u can find a solution for your problem at http://www.metabuilders.com/Tools/DefaultButtons.aspx just download the control and include it in ur project and use it to tie certain textboxes to a certain button!

Kent
September 5th, 2006

Matt,

You rock! This is exactly what I needed. I have no idea why it works or how you figured it out, but I’m glad you did…!

Tony
September 20th, 2006

Doesn’t work for me either. I think the reason why it doesn’t work is because I’m not using ASP.Net 2.0 and I have multiple button’s. The behavior is explained in this article.

http://www.ciol.com/content/developer/dotNet/2006/106080201.asp

Jason
September 27th, 2006

I tried the control from Metabuilders and it doesn’t seem to solve my issue. I have a page made up of multiple user controls (some of which have text boxes and buttons that serve different functions). How can I make each usercontrol respond to a unique default button?

Sean Campbell
October 30th, 2006

I am using ASP 1.1, this worked for me.

I am very interested in finding out why this bug exists? Are there any other ways to fix this problem?

Any insight would rock! email me at: firesickle@gmail.com (I hope I dont get more spam mail now).

sean

Rob
November 15th, 2006

This solution posted by scottgu@microsoft.com on http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx works perfectly in ASP.NET 2.0

In ASP.NET V2 you can drive the enter behavior on any focus point on a page. You set this either on the tag or by wrapping regions in an and then setting the default button property on those container controls (the asp:panel approach is how you can have multiple default buttons all over the page). If the focus is inside that region and enter is hit, the container control will cause the right post-back control to fire.

Ky
January 17th, 2007

Awesome…. this problem was a full day of searching the web for me

anon
February 13th, 2007

This worked for me in ASP.NET 1.1.

I spent days (on and off) searching for a solution, including trying a number of dodgey javascript tricks.

ketan
March 21st, 2007

thank for solution..

Ashwin
April 3rd, 2007

Was a savior. Spent a whole day on searching though. Could anyone explain me on this weird behaviour.

Armaghan Uljamil
April 9th, 2007

Thanks MAN!
You just saved me from a heck ‘lot of searching

RG GUPTA FROM DELHI
April 28th, 2007

I have problem with ASP.Net 2.0 when we pressed enter key in textbox then want to fire event submit button , if anybody know about javascript then mail to me .

Ramagovind Gupta

RG GUPTA FROM DELHI
April 28th, 2007

I have problem with javascript menu in aspx page if I have dropdownlist in page the submenu of menu goes back over the dropdownlist , if someone know about problem then mail me .

Very Very Thanks.

Ramagovind Gupta

Aswin
May 17th, 2007

Thanks.. worked like a charm..
thats what matters…

Lê Anh Tùng
June 6th, 2007

Thx very much!
This Solution work for me.
Very Simply.
But this seem to be a bug of Net 2.0

Nadeem
June 13th, 2007

This fix works.. Thanks.

But my problem was what if there are 2 buttons on the same page.
You can consider this..
Let me explain the scenario, On a forgotpassword page, I have 2 panels,
one of the panels had username textbox and a button submit in it
Second panel had a verification question and answer with a submit button in it

The Fix is: A panel has an attribute called DefaultButton so you can set it to the DefaultButton =”btnSubmit1″ for panel1 and DefaultButton = “btnSubmit2″. This should fix your problems. This attribute i found it in ASP 2.0 i m not sure about 1.1

If you use a panel, i think you can use the same attribute DefaultButton if you have a single textbox and a button.and it should work fine..

Dino Stamatiou
June 20th, 2007

Hallelujah…someone heard my prayers.

Mohit
July 19th, 2007

Works Fine in IE bt didnt work for Firefox

Chris
July 19th, 2007

It’s an amazing fix. I thank you very much!

Krutika
August 16th, 2007

I have a webform Login page which inherits from a Master page.
Web form contains 3 textboxes, 2 image buttons and validation controls for the textboxes.
The form does not get submitted when ‘Enter’ key is pressed from any of the textboxes.
I tried adding a HTML textbox on the page, but still the form does not get submitted.
What could be the cause?

Val
September 18th, 2007

I have a webform with several textboxes and required field validators for 3 of them. I also have 2 image buttons: one for submit form and second one for cancel (to return to the previous page). Submit button works fine however cancel button fires validators first. If I put something into that 3 required fields cancel then is able to proceed as expected.
How to make cancel button to bypass validators?
thanks

Val
September 18th, 2007

The solution is:

Val
September 18th, 2007

Val
September 18th, 2007

imagebutton CausesValidation=False

Petr
October 9th, 2007

Sending - “Thank you” to the author of this article. I lost couple hours because of this bug trying to figure it out!

Nima Hejazi
October 29th, 2007

Cool man, works like a charm. thank you very much.

Eric
December 2nd, 2007

Thank you for the quick fix! Hopefully .NET 3.5 will fix that problem.

reddy
December 19th, 2007

I have only one image button on my page , when i click this it saves the page ,

But ny problem is it works even if i click enter key , this should happen , i have tried in many ways . Can any one help me in this issue.(on Clicking enter key it should not take any action)

shine
December 26th, 2007

Nice effort ! Thanks

Ingrid
January 4th, 2008

That was easy!!

Danglvh
January 20th, 2008

how does it work on FireFox? It works perfectly on IE but non in FF.

Robert
March 31st, 2008

Thanks for the article, it helped me get a solution that worked in IE and FF that way I wanted it.

http://droyad.blogspot.com/2008/04/enter-key-on-linkbuttons.html

lu
April 3rd, 2008

Thank you so much. I got so frustrated with that

jasoomian
June 10th, 2008

nearly three years after the original post by Matt, and this fix still works wonderfully. Been banging my head on this off and on again for two days now.

Thanks, Matt!

joel
June 13th, 2008

ive been frustrated by this for weeks. thanks a lot man!

PK
June 18th, 2008

pls help me. Its not working for me. i have used validation control. does it make any differene??

kunal.org » Blog Archive » Enter Key -> Submit Form in ASP.NET in IE
July 18th, 2008

[...] and visibility hidden. Do not set Visible= false. Today was the first time I actually ran into this problem (It does work with an asp:TextBox unlike this link says [...]

kumar
December 10th, 2008

Hi Matt,

It worked for me thnx……..I dont understand what is the reason behind it though…….Can you shed some light on it or atleast what you think of it….

Anyway Thnx,
Kumar

Anonymous
December 23rd, 2008

This was posted in 2003, we are in 2008 and it’s still happening!?

Thanks for the solution!

sakshi
January 28th, 2009

to access any button by enter key you just have to declare it as an “accept button”…its a property of .net and works perfectly.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2009