Pressing the enter button in any text field triggers the Save event by clicking the button?

I have a standard asp.net web form with several text fields (Autopostback = False) on it and a Save button.
Pressing ENTER in any text field triggers a server click event on the Save button to invoke. If I break down and look at the call stack, the click event is the only event recorded. I do not have a standard button for the form. If I put one more button above the save button, it will receive its click event, which is fired whenever the input is pressed in the text field.

Any ideas why this is happening, and how to make it stop?

+3
source share
5 answers

This is the default behavior for most fields except text areas.

To get around this, you can call the javascript function before submitting the form to check for keystrokes.

<script type="text/javascript">
  function allowSubmission() {
  return !(window.event && window.event.keyCode == 13); }
</script>

Then the only way to submit the form is to actually click submit. However, as many people mentioned, the input key representing the form is the expected behavior, so you can always change the function to perform basic validation in the fields, allowing you to enter the key to submit the form if all the required fields are filled.

<script type="text/javascript">
  function allowSubmission() {
  return !(window.event && window.event.keyCode == 13) || validateInput(); }
</script>

Edit: You called this function using the OnClientClick method of your submit button. Sort of:

<asp:Button id="SubmitBtn" runat="server" OnClientClick="return allowSubmission()"/>
+3
source

-: , , ​​( ). , JavaScript . , !

+3

, Multiline TextMode, , , .

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>

, , JavaScript, .

,

function hookUpToEnterKey()
{
    // Hook up to read enter key
    document.onkeydown = processEntryKey;
}

function processEntryKey(e)
{

    if( !e ) 
    {
        if( window.event ) 
        {
        //Internet Explorer
        e = window.event;
        } 
        else 
        {
        // Cannot get the even return.
        return;
        }
    }

    if( typeof( e.keyCode ) == 'number'  ) 
    {
        //DOM
        e = e.keyCode;
    } 
    else 
        if( typeof( e.which ) == 'number' ) 
        {
            //NS 4 compatible
            e = e.which;
        } 
        else 
            if( typeof( e.charCode ) == 'number'  ) 
            {
                //also NS 6+, Mozilla 0.9+
                e = e.charCode;
            } 
            else 
            {
                //total failure, we have no way of obtaining the key code
                return;
            }

      if(e == 13) 
      {
         // Do nothing
         return false;
      }
}
+2

: gridview grid, . . . , , , . , , onclick , . , true autopostback . , , - . , .

+1

Your browser is trying to be a good guy and click the button for you, even if you did not set it as "default" (it will be "click" the first button on the form when you type).

As David said, if you want the input key to cause a line break in the text field, it must be multi-line

0
source

Source: https://habr.com/ru/post/1713391/


All Articles