Pressing Enter Key in a Multiline TextBox causes a Postback in FireFox

One of our authors Malcolm Sheridan informed me that when he is replying to a viewer who has left comments and enters text in the Comment field, he can't hit the enter key to start a new paragraph or a new line because doing so causes a postback in FireFox. The Comment field happens to be a Multiline TextBox and the issue is seen in the FireFox browser.

Well if you are facing a similar issue and are looking out for a solution, here it is.

<asp:TextBox ID="txtBody" onkeypress="event.cancelBubble=true;"
runat="server" Text='<%# Bind("Body") %>' TextMode="MultiLine"
Rows="5" Width="100%"></asp:TextBox>

Observe how I have used event.cancelBubble=true on the keypress event. This property determines whether the current event should bubble up the hierarchy of event handlers. Setting it to true disables bubbling for the event and does not cause a postback when the Enter key is pressed.

Read more on the cancelBubble Property over here

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Soetam Rizky said...

Just know it now that there is a property called cancelbubble....
Thx for the info....