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

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 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....