Textbox field do postback on pressing "Enter"

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

Expert Comment

ID: 16321979
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

Jason Scolaro earned 200 total points
ID: 16322283
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

Expert Comment

ID: 16323609
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."

Author Comment

ID: 16330282
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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