Prevent postback in FF invoked by textarea enter click (ASP.NET 2.0)
April 19th, 2007 by Ivan Uzunov
Recently I found new bug in MS WebResource.axd javascript. The problem is that when using a browser like Firefox and a multiline textbox is put inside panel with DefaultButton set, pressing the enter button while editing text in the textbox submits the form instead of going to new line.
The fix that I’ve found is this JavaScript function that accepts the textbox client id as parameter:
function PreventSubmitOnKeyPress(sObjectID) {
try {
if (!document.all) {
var oObject = $id(sObjectID);
if (oObject.addEventListener) {
oObject.parentNode.addEventListener("keypress",
function(e) {
if (e && e.keyCode && e.keyCode==13) {
e.stopPropagation();
}
}, false);
}
}
} catch (e) {}
}
To call the function you can use something like that:
<script type="text/javascript">ASP.NET, default button, JavaScript
var sMyTextBoxID = ‘<%=txtMyTextbox.ClientID %>’;
PreventSubmitOnKeyPress(sMyTextBoxID);
</script>




I have this problem and this multi lined textbox in a ajax updatepanel control. I already have an attached a javascript to this textbox “onkeyup”. What i haven’t understand here is how to call the javascript u written from my textbox. Can u please give me an idea. It’ll be a big help???
OJ
Hi OJ, You do not have to attach the JS to the "onkeyup" event. Just add the second script in the bottom of your page and the function PreventSubmitOnKeyPress() will do the job:
<script type="text/javascript">
var sMyTextBoxID = ‘<%=txtMyTextbox.ClientID %>’;
PreventSubmitOnKeyPress(sMyTextBoxID);
</script>
Worked like a charm, once I replaced $id with document.getElementById().