Friday, January 23, 2009

ASP.NET 2.0 Multiline TextBox submits on enter in Firefox

I'm sure I've run into this issue before. If a form contains a asp:TextBox with TextMode="MultiLine" and you press enter in the textbox to create a new line the entire form gets submitted to the server in Firefox.

In my case Firebug revealed that the textarea was nested in a asp:Panel where onkeypress="javascript:return WebForm_FireDefaultButton(event, 'ctl03_ctl05_btnSubmit')"

A little Googling on FireDefaultButton reminded me that its implementation is IE specific. The following article shows how to register a client script include that will update the function.

Multi-line Text boxes and the DefaultButton in ASP.NET 2.0

The same issue is also discussed on StackOverflow: Asp.Net Form DefaultButton Error in Firefox and StackOverflow: enter key to insert newline in asp.net multiline textbox control

function WebForm_FireDefaultButton(event, target) {
 if (event.keyCode == 13) {
 var src = event.srcElement || event.target;
 if (!src || (src.tagName.toLowerCase() != "textarea")) {
 var defaultButton;
 if (__nonMSDOMBrowser) {
 defaultButton = document.getElementById(target);
 }
 else {
 defaultButton = document.all[target];
 }
 if (defaultButton && typeof(defaultButton.click) != "undefined") {
 defaultButton.click();
 event.cancelBubble = true;
 if (event.stopPropagation) event.stopPropagation();
 return false;
 }
 }
 }
 return true;
}