Textbox field do postback on pressing "Enter"

Posted on 2006-03-29
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!
Question by:split-etm-
    LVL 4

    Expert Comment

    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 + "','')");
    LVL 22

    Accepted Solution

    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
    LVL 7

    Expert Comment

    Why not just do this:

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

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

    Author Comment

    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. :)

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now