Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 604
randomThought Avatar asked Dec 19 '09 21:12

randomThought


People also ask

How to disable enter key for a button in c#?

Set UseSubmitBehavior="False" on your Buttons.

How do I disable a button in webform?

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.


2 Answers

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.

like image 119
womp Avatar answered Sep 30 '22 22:09

womp


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.

like image 24
Jeremy McGee Avatar answered Sep 30 '22 21:09

Jeremy McGee