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

  1. says:

    Thanks, I was having this exact same problem on my page in IE (Firefox worked fine). Once I changed onKeyPress to onKeyDown it started working perfectly in both browsers.

  2. says:

    @Patrick Microsoft spend alot of effort on hiding the nitty and gritty of html and javascript into server side controls.

    But as soon as we hit real life scenario, we always still need to dig in alittle deeper.

  3. says:

    thanks for the solution, very usefull for me..

  4. says:

    Thank you, I can here the new knowledge

  5. says:

    interest js, thnk for shared