0

I know that's not a new subject but i'm stuck on this since last friday...anywho...

I've been asked to make my custom textboxes in a way that when the user hits enter, it would not genereate a postback, but the tab event instead. In my custom textbox class, i've done this in my Render method:

Me.Attributes.Remove("onkeydown")
Me.Attributes.Add("onkeydown", "if(event.keyCode == 13){return event.keyCode = 9;})

It works, only on IE. On FF, you use the Which property, but i found out that it's a read-only one. So i'm assuming that you can't set a value in this property in Chrome either.

That's my second try:

Me.Attributes.Remove("onkeydown")
Me.Attributes.Add("onkeydown", "if(navigator.userAgent.indexOf('MSIE') != -1){if(event.keyCode == 13){return event.keyCode = 9;}} if(navigator.userAgent.indexOf('Firefox') != -1){if(event.which == 13){return event.which = 9}} if(navigator.userAgent.indexOf('Chrome') != -1){if(event.keyCode == 13){return event.keyCode = 9;}}")

It works. It detects correctly which browser i'm using but when it comes to set the keycode value, only on IE works.

I've done some research and even asked a question here and i've been told that you can use a js file as an embeddedresource in a dll project. I've followed some examples and couldn't make it work. I'm using VS2005 and VB.net.

My other option was to create a new key event in javascript and try to fire it since you can't set values on which and charCode, but with no success.

i've tried to insert inline jquery as well. Didn't work either.

Before saying that inline javascript is a bad practice, i already know it is a bad practice. But these controls are being used in a huge (really...it's huge) system. The slightest change could mess things up. If it's possible i'd like to use inline javascript like i tried to use. How could i simulate the tab event using javascript? If it's not possible, could someone please explain to me how i add a js file as embedded resource?

Joao Victor
  • 1,111
  • 4
  • 19
  • 39
  • 1
    In _JavaScript_, you can't redefine keycodes and generating custom keypress events don't result in typing. To get around this, prevent the event, find _selectionEnd_ and insert your new character using string replacement methods. – Paul S. Aug 21 '13 at 11:46
  • Can you explain to me how i can do what you said? – Joao Victor Aug 21 '13 at 11:54
  • [`eventTarget.addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener), [`Event.prototype.preventDefault`](https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault), [`Event.prototype.stopPropagation`](https://developer.mozilla.org/en-US/docs/Web/API/event.stopPropagation), [_HTMLTextAreaElement_](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextareaElement) (for _selectionEnd_, same for ``), [`String.prototype.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) – Paul S. Aug 21 '13 at 12:15
  • Is not this what you are looking for? Simulate JavaScript Key Events http://stackoverflow.com/questions/596481/simulate-javascript-key-events – IvanH Aug 21 '13 at 13:06
  • i've tried this: function simulateKeyPress(character) { jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) }); } And did this: Me.Attributes.Add("onkeypress","simulateKeyPress(9)") and Me.Attributes.Add("onkeydown","simulateKeyPress(9)") I'm using jquery 1.7.2 and it still doesn't work. What i'm missing here? – Joao Victor Aug 21 '13 at 14:25

0 Answers0