Selecting Another Textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What i am doing is pretty simple. I am writing a simple control that you
probably have seen on many websites. You have a phone number field ( 3
Textboxes ) if the size of the textbox text is = 3 move to the second text
box. My problem is: How do i tell my app to select the second text box?

Here is an example on what I have so far:


Private Sub txtPhone1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtPhone1.TextChanged
If Len(txtPhone1.Text) = 3 Then
HELP!!!
End If
End Sub
 
You should probably do this on the client-side, using JavaScript, without a post-back.

If you really want to handle it in a post-back event, then you should emit script to the client to focus your textbox.

Inline:
Private Sub txtPhone1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles txtPhone1.TextChanged
If Len(txtPhone1.Text) = 3 Then

' Please forgive me that I haven't done VB.NET coding in quite a while....

Page.RegisterStartupScript( _
"txtPhone1AutoTab", _ ' name the script, uniquely
String.Concat( _ ' provide the script as a string
"<script language='JavaScript' name='txtPhone1AutoTab'>", vbCrLf, _
"document.getElementsByName(", txtPhone2.ClientID, ")[0].focus();", vbCrLf , _
"</script>"))

' Is there a literal-token in VB.NET??

' Just note that you'd probably be better off adding the registration code in the OnPreRender method and just setting a variable
here that will cause the script to be registered if the controls are visible at the time of rendering.
 
Hi there,

This is not going to work server side. You have to do it client side using
Javascript to check the size of the text in the box. I am not sure if ASP.NET
will cause a postback after every key stroke. I think it does a postback
after the box looses focus. Never the less, you don't want postbacks back and
forth every time someone types something, do you?

Good luck!
 
Back
Top