469,619 Members | 2,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,619 developers. It's quick & easy.

TextBox How To get focus on next textbox Upon Carriage Return Press In TextBox

hi all,
In my web page i have 3 text boxes and one submit button...Actually my problem is how do i focus on 2nd text box upon (pressing enter key)carriage return press in the first text box after entering some text in first text box...

Thanks in advance,
Feb 14 '09 #1
7 12217
4,871 Expert 4TB
Not sure why you would want to this, but you should be able to get this work not using a submit button and chosing the right event.
Feb 15 '09 #2
i want my project to be more flexible to the users who use my project... the user should type text in the control and soon he press enter he should be able to type text in another textbox next to the previous text box....and so on.... after the focus comes on the submit button if he press enter then the form should get submitted...
Feb 16 '09 #3
You should use tabindex property of textbox.Keep tabindex property of first textbox 1,then 2 for second textbox n so on..................
Feb 16 '09 #4
thanks for reply,,,if i put tab index for each text box it works well if the user press tab key.. but i want the same functionality when user press enter key....
Feb 16 '09 #5
Give onkeydown property for all your textboxes like this:

<asp:TextBox ID="name" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
Feb 20 '09 #6
I hope you know got the solution of ur problem.
Feb 20 '09 #7
9,735 Expert Mod 8TB
Hi there,

Alamodgal's suggestion is correct however not very clear.

You need to use JavaScript in order to control what is happening in the web browser (client side) while user is using your page.

You need to write a JavaScript function that will capture which key the user pressed, check whether that key was the enter key, and either move to the "next" text box or submit the form.

So, taking a further look at Alamodgal's suggested code:
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="name" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>  
Here Alamodgal has specified some code that handles the "onkeydown" JavaScript event for the TextBox.

Right now the code checks if the user has pressed the enter key (an ascii value of 13) but it doesn't do anything.

If I were you I'd create a JavaScript function to handle the "onkeydown" events for each textbox on the page instead of supplying code inline like Alamodgal suggested.

Something like (JavaScript code):
Expand|Select|Wrap|Line Numbers
  1. function MoveToNextTextBox(e)
  2. {
  3.    //Determining what key was pressed
  4.     var keynum;
  5.     if (window.event || e.keyCode) {
  6.         keynum = e.keyCode;
  7.     }
  8.     else if (e.which) {
  9.         keynum = e.which;
  10.     }
  12.     //checking if the key pressed was the enter key (13)
  13.     if(keynum == 13)
  14.     {
  15.         //in here "move to the next TextBox"
  16.     }
  18. }
Now the thing is, you have to keep track of which TextBox currently has focus and you have to use that to determine which TextBox to move to "next". So, now you need to add a function that you can call on the "onfocus" JavaScript event of each TextBox. This function will indicate which TextBox has the focus so that you can determine which TextBox to move to next in the "MoveToNextTextBox()" JavaScript method.

So now your JavaScript is going to look something like:

Something like (JavaScript code):
Expand|Select|Wrap|Line Numbers
  2. var textBoxWithFocus; //<-- will hold a reference to the text box with the current focus.
  4. function HasFocus(TextBox)
  5. {
  6.    textBoxWithFocus = TextBox;
  7. }
  9. function MoveToNextTextBox(e)
  10. {
  11.    //Determining what key was pressed
  12.     var keynum;
  13.     if (window.event || e.keyCode) {
  14.         keynum = e.keyCode;
  15.     }
  16.     else if (e.which) {
  17.         keynum = e.which;
  18.     }
  20.     //checking if the key pressed was the enter key (13)
  21.     if(keynum == 13)
  22.     {
  23.         //in here "move to the next TextBox"
  24.       }
  26. }
And your TextBox would look something like:

Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="name" runat="server" onkeydown="MoveToNextTextBox(event)" onfocus="HasFocus(this)"></asp:TextBox>  
This should get you started.

Some other things that you may be interested in that could possibly help you with your solution are:
Feb 23 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by VMI | last post: by
7 posts views Thread by Nathan | last post: by
2 posts views Thread by dancer | last post: by
3 posts views Thread by HockeyFan | last post: by
11 posts views Thread by evenlater | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.