Asp.net 1.0 / 1.1: Enter key for multiple forms

In asp.net 1.0 and 1.1, there seems to be no easy way of having multiple forms on a webpage that each perform separate actions when you press the enter button from within them (instead of clicking a button to postback to the server). If you press enter anywhere, the click event for the first button added to the form is run. In standard html, this would easily be accomplished by having two separate forms, however you cannot have two separate forms in asp.net.

Note that this is solved by dividing the form up with panels and specifying the defaultbutton attribute for each panel in .net 2.0

To solve this, I added some js to the onkeydown event on my textbox on my second form. Note that this works with both firefox and i.e. (hence event.which and event.keyCode).

myTextBox.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+ myButton.UniqueID + "').click();return false;} } else {return true}; ");

Some related articles are as follows:
http://www.codeproject.com/useritems/FormPanel.asp
http://www.dotnet247.com/247reference/msgs/5/26773.aspx
http://www.codeproject.com/useritems/aspnet_Enter_key_problem.asp
http://www.jimzimmerman.com/blog/CommentView,guid,dfa0280f-a20e-4c47-8b39-bf7cb83e3849.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>