Submitting ASP.NET form with ENTER key
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.
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 …
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
RE: *MORE* ON ASP.NET: How to create a Default
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
Thank you! Thank you! What a bizarre and annoying bug.
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
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. :-)
IF you added AutoPostBack=”true” in the asp:textbox would it not send back as soon as the value changes?
I don’t get it, this code did not work for me. I’m still unable to send my form using the Enter key.
Thanks so much for this fix. I’ve been searchin for quite some time for this.
:)
Why have I not seen this anywhere else? It’s such a simple fix. Thanks for the help.
Ok - this invisible text box works, but can anyone explain WHY?
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.
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.
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…
Submitting ASP.NET form with enter key
Cool Beans!
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!
This is awesome. Thanks so much for the post. It worked like a champ!!!
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
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.
Great….Thanx a lot….
This freebie fixed the ascx problem for me. http://www.metabuilders.com/Tools/DefaultButtons.aspx
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
Thank you! It’s really the simplest method according to me.
It is a great fix.
A lot of client scripts have been suggested on the net, but they didn’t work for me
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();
}
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,”) .
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
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.
This is why this fix works:
http://aspnet.4guysfromrolla.com/articles/060805-1.aspx
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
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.
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
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.
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
Thanks, I never thought that this problem will have such a twisting solution
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!
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…!
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
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?
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
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.
Awesome…. this problem was a full day of searching the web for me
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.
thank for solution..
Was a savior. Spent a whole day on searching though. Could anyone explain me on this weird behaviour.
Thanks MAN!
You just saved me from a heck ‘lot of searching
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
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
Thanks.. worked like a charm..
thats what matters…
Thx very much!
This Solution work for me.
Very Simply.
But this seem to be a bug of Net 2.0
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..
Hallelujah…someone heard my prayers.
Works Fine in IE bt didnt work for Firefox
It’s an amazing fix. I thank you very much!
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?
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
The solution is:
imagebutton CausesValidation=False
Sending - “Thank you” to the author of this article. I lost couple hours because of this bug trying to figure it out!
Cool man, works like a charm. thank you very much.
Thank you for the quick fix! Hopefully .NET 3.5 will fix that problem.
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)
Nice effort ! Thanks
That was easy!!
how does it work on FireFox? It works perfectly on IE but non in FF.
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
Thank you so much. I got so frustrated with that
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!
ive been frustrated by this for weeks. thanks a lot man!
pls help me. Its not working for me. i have used validation control. does it make any differene??
[...] 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 [...]

Thank you! This works perfectly.