Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 369
  • Last Modified:

Automatically move to next textbox

Hi Experts,

I have 7 textboxes in asp.net (VB).  The purpose is to allow one letter be placed in each box and then move to the next box automatically.  I have tried to use the textbox_TextChanged event but cannot get it to work.  

My code is shown below and I can not get it to fire.

Any Ideas?

Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        If Len(Let1.Text) = 1 Then
            Let2.Focus()
        End If
    End Sub

Open in new window

0
us1975mc
Asked:
us1975mc
  • 6
  • 6
  • 2
  • +1
1 Solution
 
strickddCommented:
Switch to javascript. Add an "onchange" event to each textbox and have it call the focus of the next textbox:

function(tbToFocus)
{
     document.getElementById(tbToFocus).focus();
}
0
 
strickddCommented:
Code-Behind
Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")
'etc

Code-InFront
<script type="text/javascript">
function FocusNextTextBox(tbToFocus)
{
     document.getElementById(tbToFocus).focus();
}
</script>
0
 
eridanixCommented:
Hi,

I think, you have to set focus to another textbox control with javascript.

Some useful example you can see here http://ryanfarley.com/blog/archive/2004/12/21/1325.aspx

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
us1975mcAuthor Commented:
strickdd,

I am not for sure where you are saying to place hte code behind.  My textbox has DataBinding, Disposed, Init, Load, PreRender, TextChanged and Unload.  The only one that I figured to place it on was the TextChange and that don't work.
Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
    End Sub

Open in new window

0
 
us1975mcAuthor Commented:
eridanix,

The web site that you sent me to was all in C#.net.  I need VB.net.
0
 
guramritCommented:
TextBox.TextChanged is server side event, it will not occur when you changed the text, but it will occur when you changed the text and press any button to PostBack changes.
You've do similar code in client side (javascript) as told by others.

You've to add following lines in Page_Load event instead of Let1_TextChanged
 Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
0
 
strickddCommented:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
End Sub
0
 
strickddCommented:
Sorry, remove the "Handles Let1.TextChanged", basically this goes in the Page_Load method.
0
 
us1975mcAuthor Commented:
I have listed the code that I have in the code behind and a image of the error that I am receiving.  Any ideas? This is the error that I am getting
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
        Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")

    End Sub

Open in new window

0
 
strickddCommented:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")
        Let2.Attributes.Add("onchange", "FocusNextTextBox('" & Let3.ClientID & "')")

    End Sub

-- Added single quotes around the parameter in the function call.
0
 
guramritCommented:
replace
Let1.Attributes.Add("onchange", "FocusNextTextBox(" & Let2.ClientID & ")")
 Let2.Attributes.Add("onchange", "FocusNextTextBox(" & Let3.ClientID & ")")
with
Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")
Let2.Attributes.Add("onchange", "FocusNextTextBox('" & Let3.ClientID & "')")

The difference is single qoute before and after " & Let2.ClientID & "
0
 
us1975mcAuthor Commented:
Still a no go. Typing a letter in the text box does not move to the next textbox.  When I place a break on Let1.Attributes.Add("onchange", "FocusNextTextBox('" & Let2.ClientID & "')")  and hover over ClientID I get "ctl00_ContentPlaceHolder1_Let2".  Is that what I should be getting there?
0
 
strickddCommented:
Yes, that is the ClientID of Let2. That is fine. Alert the document.getElementById line without the .focus()
0
 
us1975mcAuthor Commented:
I changed the onchange to onkeyup and it works.

Let1.Attributes.Add("onkeyup", "FocusNextTextBox('" & Let2.ClientID & "')")
Let2.Attributes.Add("onkeyup", "FocusNextTextBox('" & Let3.ClientID & "')")

Thanks for the help!
0
 
us1975mcAuthor Commented:
Thanks again!

us1975mc
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now