We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Textbox field do postback on pressing "Enter"

split-etm- asked
Medium Priority
Last Modified: 2008-01-09

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!
Watch Question

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 + "','')");
Top Expert 2006
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):

-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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."


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. :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.