Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus After Last Letter Typed 4

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
Hello
I have a .net page that uses a textbox as an event to do a postback and call a webservice that returns a dataset and binds it to a datagrid

my only problem is that I call the post back OnKeyUp and when i try and direct focus back to the textbox it is pushed to the first letter in the box and when you hit an arrow key to move or hit backspace the onkeyup event fires and the process starts again;(

so.. how can I direct focus to after the last letter in textbox and how can I trap the backspace key so this won't fire?

Code:
<script type="text/javascript" language=javascript>
  onload=function(){
  document.getElementById("TextBox4").focus();
  }
</script>

Thanks in advance for any help or advice and sorry in advance if im not being clear as to what i want or If I dont sound educated enough in the matter at hand

MCP, .Net Solutions Development <%_%>
 
try googling for "createTextRange javascript" and it should get you started on the right track. Most cursor/selection manipulation can be handled with that command. Additionally, I think it only works in IE so you'll have to search for a firefox equivalent.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Thanks Ive adjusted my code as follows and it works. But im still wondering how I can trap the arrow and backspace keys any thoughts on a helpful method?
Code:
<script type="text/javascript" language=javascript>
  onload=function(){
  document.getElementById("TextBox4").focus();
  textboxSelect(document.getElementById("TextBox4"));
  }
 function textboxSelect (oTextbox){
  var oRange = oTextbox.createTextRange();
           oRange.moveStart("character",oTextbox.value.length);
           oRange.moveEnd("character",oTextbox.value.length); 
           oRange.select();
           break; 
}
</script>


MCP, .Net Solutions Development <%_%>
 
You can discrimine keys by its keycode when using onKeyUp.

Cheers,
Dian
 
Here is example of how i have done this type of thing in the past. It can filter out characters like tab, enter, etc and it takes into account both ie and firefox.

Thanks,
Chris

<code>
// Example:
// onkeyup=""AutoPhoneTab(this,3,txtPrimeprefix,event);""
//******************************************************************************/
function AutoPhoneTab(oElem, intElemLength, oNextElem, e)
{

isIE=document.all;
isNN=!document.all&&document.getElementById;

if (oElem)
{
var keyCode = (isNN) ? e.which : window.event.keyCode;
var filter = (isNN) ? [0,8,9,13,16] : [0,8,9,16,17,18,37,38,39,40,46];
//16 shift
// 9 tab
// 13 enter

if (oElem.value.length == intElemLength && !containsElement(filter,keyCode) && !e.shiftKey) //see if all digits are entered
{
if(oNextElem)
{
window.status =keyCode;
oNextElem.focus(); //move to the next textbox
oNextElem.select(); //highlight text in box
return;
}
}
}

}

function containsElement(arr, ele)
{
var found = false;
index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}



</code>
 
There are some issues with key trapping you should be aware of. They may not present a problem to you in your situation but if you know about them it might save you a lot of hair pulling. :)

Mostly the issues revolve around control key combinations but they may spill over into what you are doing.

You can use onkeypress to trap control key presses on the page but NOT when the keypress is within an input field.
onkeyup and onkeydown can trap control key combinations in input fields but you may not get the return codes that you expect. At least in Internet Explorer the value returned from the onkeyup and onkeydown events is a constant mapping so that pressing the letter A will always return a value of 65 even if it was a lower case A that was pressed rather than a capital.

For the most part this should not affect you as you try to filter out arrow and backspace keys. If however you have to test for a Ctrl-V (paste) event to make sure they are not pasting data rather than typing it directly then you will have trouble.

In my case I was trying to set a filter mask on input fields so that only legally allowed characters appear in the input field. To be perfect this has to also prevent Ctrl-V pasting as well as right-click menu paste and Edit menu paste possibilities.
To be able to accurately detect a Ctrl-V combination I had to use both a page level onkeypress event and an onkeup event at the input field level. The onkeypress event would grab the actual value of the keys pressed and store it in a variable then the onkeyup event would fire so that the control key could be detected. Unfortunately this means that the paste would occur first and then I would have to remove the pasted data from the field whereas non control key combinations could be filtered so they never posted to the field in the first place.

But you should keep in mind the limitations of the onkeypress and onkeyup/onkeydown events as you try and filter keypress events in your script.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top