Set focus to a text box on a web page after textchanged event...

Bran-Damage
Bran-Damage used Ask the Experts™
on
I have an aspx page that has text boxes.  On one of my text boxes I have a changedText event.

After the changed text event the cursur goes to an undesirable location.

I tried to do a me.txtph.focus() but that did not work.

What I am looking to do is either go to me.txtph or put the cursur when they hit tab to the next tab order or when they click on a different field, keep the cursur there.

Any ideas would be helpful.

I have attached my changed text event code.
Protected Sub txtstate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

            If Me.txtstate.Text = "AE" Or Me.txtstate.Text = "AA" Or Me.txtstate.Text = "AP" Then

                If TxtdelDoc.Text <> "" And TxtFromCountry.Text <> "" And txtcountry.Text <> "" Then
                    btnInternational.Enabled = True
                End If

            Else
                btnInternational.Enabled = False
            End If
            Me.txtph.Focus()

        Catch ex As Exception

        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Full Stack Developer
Commented:
Protected Sub txtstate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try

            If Me.txtstate.Text = "AE" Or Me.txtstate.Text = "AA" Or Me.txtstate.Text = "AP" Then

                If TxtdelDoc.Text <> "" And TxtFromCountry.Text <> "" And txtcountry.Text <> "" Then
                    btnInternational.Enabled = True
                End If

            Else
                btnInternational.Enabled = False
            End If
Page.RegisterStartupScript("SetFocus", _
"<script>document.getElementById('" & txtph.ClientID & _
"').focus();</script>")

         
        Catch ex As Exception

        End Try
    End Sub



Robb HillSenior .Net Full Stack Developer

Commented:
That was jscript solution to ....but you can try this too I think.

use Page.SetFocus method with control's ID as a parameter
Amit TripathiSharePoint Architect
Commented:
Just Paste This code After HTML End and replace Your Textbox with TextBox1...It will work 100%

<script language="javascript">
document.getElementById("TextBox1").focus();
</script>

Author

Commented:
Both of these solutions work, but my main issue, I found out, was not actually with the java script.  There was an auto postback occurring that was causing the set focus to go to another area than desired.

I modified this, used your java script... BOOM, worked like a charm

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial