disabling default button or enter key in asp.net c#

后端 未结 4 594
Happy的楠姐
Happy的楠姐 2021-01-11 14:33

I have a form where users scan in a barcode, the barcode reader automatically enters a carriage return in the value causing the form to submit since the browser chooses the

相关标签:
4条回答
  • 2021-01-11 15:03

    Set the TextBox to have the text mode property set to "multiline". Then the carriage return will be in the TextBox.

    See also here for a note about what to do if you're using FireFox.

    0 讨论(0)
  • 2021-01-11 15:04
        private void textbox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
    

    // do nothing

            }
        }
    

    this may help you

    0 讨论(0)
  • 2021-01-11 15:04

    handle the onkeypress event and do something like this

     if (e.KeyChar == (char)Keys.Enter)
     {
         // set event handled
         e.Handled = true;
     }
    
    0 讨论(0)
  • 2021-01-11 15:25

    You'll need to do it with javascript. In your markup for the text box, add an onkeydown handler like so:

    <asp:TextBox ID="TextBox1" runat="server"
       onkeydown = "return (event.keyCode!=13);" >
    </asp:TextBox>
    

    This will return false if the key was the enter key, which will cancel the form submission.

    0 讨论(0)
提交回复
热议问题