By default, if you press enter in any textbox, the default button specified for that Panel or form etc. gets fired automatically. Even if no default button is specified, the first button or ImageButton gets clicked. This is ok in most of the situations where we want to submit the form or want the buttons/imagebuttons to get clicked. But in some cases you don’t want any Button to get clicked when you press enter in any textbox and that’s a problem. For asp.net Button controls you can simply set the UseSubmitBehavior="False" and get away with them. But I could not find any similar property of ImageButton control to stop it from getting clicked.
Prevent any button or imagebutton from getting clicked when you press enter anywhere in the browser window, though you still want their click events to get fired when you click them with the mouse or navigate & click using keyboard etc.
In my case, I had some buttons and imagebuttons that shows some popup windows when clicked. But the problem was that if I pressed enter on any of my textboxes, a popup window just popups up which is annoying because we didn’t really click the related button/imagebutton.
One way (which works only if you have buttons and no imagebuttons on your form) is to set UseSubmitBehavior="False" for all the buttons. If there are too many buttons on your form or you want to do conditionally, this function may be handy.
'Call the function in form load or other appropriate event like this DisableEnterKeyPress(Me) Private Sub DisableEnterKeyPress(ByVal control As Control) For Each ctl In control.Controls If ctl.HasControls Then DisableEnterKeyPress(ctl) ElseIf TypeOf ctl Is Button Then ctl.UseSubmitBehavior = False End If Next End Sub
But unfortunately that didn’t work for me because I had ImageButtons too on my form; and ImageButtons have no built in way to disable that behavior. I tried trapping the onkeypress / onkeydown events of the ImageButtons but that doesn’t work either.
After failing in one way or the other using straight forward code, this idea stuck me.
So this is more of a workaround than straight code but should work in almost all cases. And this solution works for both Buttons as well as ImageButtons or a mixture of them as in my case.
Insert this inside your <form></form> tags, but before any Buttons or ImageButtons. To be sure that it will always work, I inserted it just immediately after the <form> begin tag.
That’s just a sweet simple one liner. Isn’t it?
How this Works
By now you must have already understood the trick and why I did what I did.
We insert an html input button before any other buttons on the form. So this becomes the first button on the form and when enter is pressed in any textbox, this button’s click event will be fired. But in the click event we just ignore it by returning false and preventing form submit.
An asp.net button would have also done equally good. But we don’t want this dummy button in the code behind. It serves no purpose there and may instead add to the confusion there. So we are content with just an html input submit type button, which is invisible on the screen because of the style we set there.