Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

.NET and form submission on "enter"

I have a webform with a submit button and also a few <input type="text"...>

If the user is typing in one of the input fields and hits return, the form gets submitted.

However, VS.NET eventual calls the click-handler for the submit button even though it wasn't pressed.

Is there any reliable way for me to check to see if the user posted the form through a carriage return or through clicking the submit button?

Charles Reich
Tuesday, May 13, 2003

I don't think there's an easy way.  I ran into this issue on a project and we solved it by writing a class to put javascript on the page to map the Enter key (and Esc key) to the buttons of our choice, or none at all.  I'd be interested in a better way around this issue, though, if there is one.

Wade Winningham
Tuesday, May 13, 2003

You might want to question why you need to know the difference. If a web app performed differently depending on if I hit enter vs. hitting submit I'd be very confused.

Chris Tavares
Tuesday, May 13, 2003

I concur with Chris generally. The folowing doesn't explicitly solve your problem as stated, but might just help, depending on what you're trying to achieve: http://www.metabuilders.com/Tools/DefaultButtons.aspx

Duncan Smart
Wednesday, May 14, 2003

Thanks for the feedback.

I understand Chris & Duncan's feedback regarding usability of forms. Here's what I was thinking about...

My page is a table made up of a thin left column which has let's say a search button and text input box.  My larger right column has several text input boxes and a submit button of its own.

ASP.NET makes the whole page one big form.

So if the user hits enter while in the search box, I want the server to run the search code.  If the user hits enter while in the form on the right, I want to server to run the form submission code.

My "solution" seems similar to the metabuilders solution that Duncan refers to which is basically to use client side javascript to catch every keystroke and decide what to do before posting back to the server.  I actually used"onkeypress" and check every time to see if the user pressed "enter."

Charles Reich
Wednesday, May 14, 2003

My thought is that a consistent navigation bar on the left side of the page would make for a clearer Web design.

Charles Reich
Wednesday, May 14, 2003

Your other option is to use two forms. The asp.net server form control is just another control.

The April 2003 issue of asp.netPro magazine had a great article on how to build a new form control that does let you have multiple separate forms on the same aspx page. The motivation was exactly what you're talking about - a search form and some other form on the same page.

Unfortunately you have to pay to get the article.

Chris Tavares
Friday, May 16, 2003

You can have two form controls, but only one of them can be a server side form. There is nothing stopping you from doing :

<form action=searchpage.aspx method="get">

<input type=text name=searchterm>
</form>

<form runat=server">

rest of aspx controls

</form>

Your entire page doesn't need to be a server side form, in fact it is a whole lot better for your client if it is not (less viewstate data to transfer).

Damian
Saturday, May 17, 2003

FYI, the browsers are responsible for choosing which button to "press" when you hit enter in a text box.  Both Mozilla and IE will simulate a click on the first visible button on the form.

Therefore, the only way to remedy the situation is on the client.  You can either

1) Put a dummy button as the first button on the form.  I use a button with "Refresh Page" as the text.  Clicking the button does nothing.  Naturally,  you don't want your users to see this ugly button if they don't have to, so I use CSS to absolutely position it 500 pixels off the left side of the page.  CSS capable browsers don't see the button and non-CSS browsers see a "Refresh Page" button at the top.

2) Capture the enter key with JavaScript.  The DefaultButton control from MetaBuilders works very well.

I use both, since people may turn off JavaScript.

Richard Ponton
Friday, May 23, 2003

*  Recent Topics

*  Fog Creek Home