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?
A:
private void textbox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// do nothing
}
}
this may help you
Kubi
2009-12-19 21:44:02
That would work fine if this was Windows Forms - alas, it's ASP.NET.
Jeremy McGee
2009-12-19 21:46:32
yeah that's right !
Kubi
2009-12-19 21:50:07
+5
A:
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.
womp
2009-12-19 21:45:15
A:
handle the onkeypress event and do something like this
if (e.KeyChar == (char)Keys.Enter)
{
// set event handled
e.Handled = true;
}
Tony
2009-12-19 21:45:43
the javascript answer should work. Here's a thread that explains why these events aren't available: http://aspadvice.com/forums/thread/27557.aspxKind of obvious if you think about it.
Tony
2009-12-19 22:22:46
+1
A:
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.
Jeremy McGee
2009-12-19 21:47:56