ASP.Net Panel DefaultButton interfering javascript onKeyPress

Symptoms

OnKeyPress doesn’t work for any objects inside an ASP.Net Panel with DefaultButton specified.
The problem only occur in IE 7 and not Firefox 3. Haven’t tried all the other browsers.

The Problem

Here’s the example:




	
	

	

So basically, I’ve got a textbox that only allow the user to type numeric values, and simply ignore other key presses. To do that I use the onkeypress event of the DOM.

At the same time, I want to listen to ‘enter’ key as well, so at anytime the user presses enter key, the submit button is ‘pressed’. To do that I simply use out of the box ASP.Net Panel DefaultButton property.

I looks like ASP.Net defaultButton uses the same onKeyPress event handler hook up. Although my javascript (return passOnlyNumericValidation()) gets executed, it ignores the return value.

The solution

I find the solution to simply replacing onKeyPress to onKeyDown. So in the example above, I have the following:



	
	

	

On that note, onKeyUp wouldn’t work because I want to ‘filter out’ the key presses, and by OnKeyUp, the character is already printed out on the DOM.

For more information about the differences between onKeyPress, onKeyUp and onKeyDown, look at the following:
KeyPress, KeyDown, KeyUp – the difference between javascript key events


About this entry