Textbox field do postback on pressing "Enter"

Hi,

This is probably more an annoyance that a problem, but is there any way to disable an ASP.NET textbox control from doing a postback when punching the Enter key while the textbox is in focus.
For instance, on a login page, i type the username & password in the respective fields, but unless i actually click the submit button, the page just reloads.

I remember that in ASP 3.0 punchine the Enter key would automatically activate the first button control in the form.

many thanks!
split-etm-Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

noamattdCommented:
Just add this to the attributes of whichever text box you want to do a postback from:

TextBox.Attributes.Add("onKeyPress", "javascript:if (event.keyCode == 13) __doPostBack('" + Button.UniqueID + "','')");
Jason ScolaroCommented:
Hi split-etm-,

This is made incredibly easy with ASP.NET 2.0.  This article discusses both ASP.NET 1.1 and 2.0 (see bottom of page for 2.0 solution):
http://www.beansoftware.com/ASP.NET-Tutorials/Accept-Enter-Key.aspx

-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wtconwayCommented:
Why not just do this:

TextBox.Attributes("onKeyUp") = "javascript:if (event.keyCode==13){btnLogin.click();}")

And it will submit the form by simulating the login button being clicked. If it has trouble locating the button just use this:

TextBox.Attributes("onKeyUp") = "javascript:if (event.keyCode==13){document.getElementById('btnLogin').click();}")

and that will single out just that one button in case you should have other controls by the name of "btnLogin."
split-etm-Author Commented:
noamattd: thanks for your post. your method worked fine, but caused a javascript error to appear in IE though because for some reason the __doPostBack javascript event wasnt available once the page was built & run.

wtconway: thanks for your post. your method also worked, but caused my validation box to popup twice for some reason.

i finally got it working 100% using code i found on the link scolja posted. thanks for that scolja.

thanks to all for the replies. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.