about software development

Fix ASP.Net form submit behavior with jQuery

In standard ASP.Net web form pages there’s only one form tag for the entire page. This unfortunately has some side effects on form submit behavior.

In a standard HTML all forms are contained in their own form tag. When the browser receives a enter key press for the form the form is submitted. Because of the single form in a ASP.Net web form this behavior will be broken and the first submit button on the page will always be triggered by the browser when pressing enter.

Microsoft has included a feature in ASP.Net 2.0 to overcome this problem. In short you add an attribute DefaultButton, with the ID of the button to trigger, to an ASP Panel control that wraps the form. Unfortunately this solution doesn’t work in Firefox.

To fix this, I decided to use jQuery. What we need to have is a way to identify the different forms on a page and connect them with the right submit button. So I added a fieldset tag around the single form:

This script finds all fieldset elements containing the defaultsubmitbutton attribute, locates all textboxes and password fields within that fieldset and hooks up the keydown event.
When the enter key is pressed (keycode 13) the default event is canceled and depending on the type of button the right postback method is triggered.