tags:

views:

7932

answers:

9

I have some C# / asp.net code I inherited which has a textbox which I want to make multiline. I did so by adding textmode="multiline" but when I try to insert a newline, the enter key instead submits the form :P

I googled around and it seems like the default behavior should be for enter (or control-enter) to insert a newline. Like I said I inherited the code so I'm not sure if there's javascript monkeying around or if there's just a simple asp.net thing I have to do.

A: 

I suspect it's (like you say) some custom javascript code.

The original asp.net control works fine... you are going to have to check the code

Juan Manuel
A: 

Are you handling the textchanged event for the textbox? That would mean ASP.Net sets the textbox to cause a postback (submit the page) for anything the might cause the textbox to lose focus, including the enter key.

Joel Coehoorn
+1  A: 

I created a sample page with a TextBox and a Button and it worked fine for me:

<asp:TextBox runat="server" ID="textbox1" TextMode="MultiLine" />
<br />
<br />
<asp:Button runat="server" ID="button1" Text="Button 1" onclick="button1_Click" />

So it most likely depends on either some other property you have set, or some other control on the form.

Edit: TextChanged event is only triggered when the TextBox loses focus, so that can't be the issue.

samjudson
+1  A: 

I can't find that "WebForm_FireDefaultButton" javascript anywhere, is it something asp.net is generating?

Yes.

That's generated to support the DefaultButton functionality of the form and/or Panel containing your controls. This is the source for it:

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;
}
Dave Ward
A: 

@dave-ward, I'm not good with javascript, is that saying it shouldn't submit the form if it's enter pressed in a textarea?

assuming that's the only javascript concerned with this textbox control, how do I solve my original problem?

adambox
A: 

That's correct.

I don't think it's related to your problem. I can confirm that using the DefaultButton property on either the form, Panel, or both does not interfere with entering a CR in textareas.

Are you absolutely sure there's no JavaScript on the page (or included elsewhere) that attaches event handlers?

Dave Ward
A: 

@dave-ward, I just dug through mounds of javascript. most was ASP.NET generated stuff for validation and AJAX, there's a bunch starting with "WebForm_" that I guess is standard stuff to do the defaultbutton, etc. the only javascript we put on the page is for toggling visibility and doing some custom validation...

edit: I did find the below. I don't understand it though :P the beginning of the form the textarea is in, and a script found later: (note, something on stackoverflow is messing with the underscores)

<form name="Form1" method="post" action="default.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="Form1">

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>
adambox
+3  A: 

It turns out this is a bug with Firefox + ASP.NET where the generated javascript for the defaultButton stuff doesn't work in Firefox. I had to put a replacement for the WebForm_FireDefatultButton function as described here:

function WebForm_FireDefaultButton(event, target) {
    var element = event.target || event.srcElement;
    if (event.keyCode == 13 &&
    !(element &&
    element.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;
}
adambox