Attack Work TM
Productivity tools to accomplish work faster
 
Username Password
   
Password help
Create new account
Skip Navigation Links.

Asp.net - Textarea Cancel Enter or Return Key that Submits a Form on FireFox

Steal This Site [Web Developer Blog]


When you set a default button using Asp.Net, have a Textarea on the form and are using FireFox a user can't hit the enter key to create a new line. Instead what happens is the form tries to submit itself. With every other input field this is desired but with a Textarea you want the user to be able to press the enter key to get a new line instead of submitting the form.

Some examples I've seen simply kill the enter key anywhere it's used on the form. That means you can't have the submit behavior work at all on the page or even have the enter key create a new line. The trick is to use cancelBubble on the Textarea itself. CancelBubble will tell every parent element to ignore this input while still allowing the key press into the Textarea.

Code:
<asp:TextBox ID="MyTextBox" TextMode="MultiLine" runat="server" onkeypress="return KeyPressReturnCancelBubble(event);" />
Code:
<script language="javascript" type="text/javascript">
    function KeyPressReturnCancelBubble(event) {
        var intKeyCode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
        if (intKeyCode == 13) {
            event.cancelBubble = true;
        }
        return true;
    }
</script>

Subscribe in a reader
Michael Khalili | Sunday, March 01, 2009
Comments
All guest comments are moderated.
Be the first to comment on this post
Post your comment
Guest Name* !
Email Address* !(Not published)
Website !
Comment*
!
*Required fields

Checkout my new project iPhone News and App Directory - Yappler.com


Terms of Service | Privacy Policy | Steal This Site
Attack Work and Voxback are trademarks of Hillside Holdings.