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 first button as the default. How can I disable anything from happening when the enter key is pressed when entring a value in that textbox?
Set UseSubmitBehavior="False" on your Buttons.
If you just want to disable it and nothing else, then add attribute Enabled = "false". ImageButton1. Enabled = false; If the form is located in the master page, then you'll have to access that control from the content page via code behind and set the Enabled attribute.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With