Solved

Automatically move to next textbox

Posted on 2011-09-07
15
342 Views
Last Modified: 2012-05-12
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
Comment
Question by:us1975mc
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36495944
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
 
LVL 28

Expert Comment

by:strickdd
ID: 36495978
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
 
LVL 5

Expert Comment

by:eridanix
ID: 36495988
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
 

Author Comment

by:us1975mc
ID: 36496532
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
 

Author Comment

by:us1975mc
ID: 36496556
eridanix,

The web site that you sent me to was all in C#.net.  I need VB.net.
0
 
LVL 4

Expert Comment

by:guramrit
ID: 36496766
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
 
LVL 28

Expert Comment

by:strickdd
ID: 36496993
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:strickdd
ID: 36497002
Sorry, remove the "Handles Let1.TextChanged", basically this goes in the Page_Load method.
0
 

Author Comment

by:us1975mc
ID: 36497160
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
 
LVL 28

Expert Comment

by:strickdd
ID: 36497192
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
 
LVL 4

Expert Comment

by:guramrit
ID: 36497199
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
 

Author Comment

by:us1975mc
ID: 36497683
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
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36497897
Yes, that is the ClientID of Let2. That is fine. Alert the document.getElementById line without the .focus()
0
 

Author Comment

by:us1975mc
ID: 36498091
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
 

Author Closing Comment

by:us1975mc
ID: 36498099
Thanks again!

us1975mc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now