I suspect that I am making this much harder than necessary...
Consider a form with four TextBox'es: txtFName, txtMName, txtLName and txtUserName. The first three have Autopostback=true. The UserName is created algorithmically from the user's name.
The problem is this: when I enter a name in one of the first three text boxes, and then hit tab (which causes a reload due to the autopostback), the focus stays in the box and does not advance.
Here is the code I have put together - but its does not work as desired. What am I doing wrong? (I have stepped through this and everything seems OK - it just doesn't set the focus where I want upon reloading the page.)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (IsPostBack) Then
If (Request.Params("__EVENTTARGET"
.ToString = "txtFName"
Then
showUserName()
SetFocus(txtMName)
ElseIf (Request.Params("__EVENTTARGET"
.ToString = "txtMName"
Then
showUserName()
SetFocus(txtLName)
ElseIf (Request.Params("__EVENTTARGET"
.ToString = "txtLName"
Then
showUserName()
SetFocus(txtEmail)
End If
Else
' do first time stuff
End If
End Sub
Private Sub showUserName()
' do dtuff to determine the username
txtUserName.Text = LCase(strUserName)
End Sub
Private Sub SetFocus(ByVal ctrl As Control)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javascript'>document.getElementById('" + ctrl.ClientID + "').focus();</script>"
' Add the JavaScript code to the page.
Page.RegisterStartupScript("FocusScript", focusScript)
End Sub
----
Gerry Roston
gerry@pairofdocs.net
Consider a form with four TextBox'es: txtFName, txtMName, txtLName and txtUserName. The first three have Autopostback=true. The UserName is created algorithmically from the user's name.
The problem is this: when I enter a name in one of the first three text boxes, and then hit tab (which causes a reload due to the autopostback), the focus stays in the box and does not advance.
Here is the code I have put together - but its does not work as desired. What am I doing wrong? (I have stepped through this and everything seems OK - it just doesn't set the focus where I want upon reloading the page.)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (IsPostBack) Then
If (Request.Params("__EVENTTARGET"
showUserName()
SetFocus(txtMName)
ElseIf (Request.Params("__EVENTTARGET"
showUserName()
SetFocus(txtLName)
ElseIf (Request.Params("__EVENTTARGET"
showUserName()
SetFocus(txtEmail)
End If
Else
' do first time stuff
End If
End Sub
Private Sub showUserName()
' do dtuff to determine the username
txtUserName.Text = LCase(strUserName)
End Sub
Private Sub SetFocus(ByVal ctrl As Control)
' Define the JavaScript function for the specified control.
Dim focusScript As String = "<script language='javascript'>document.getElementById('" + ctrl.ClientID + "').focus();</script>"
' Add the JavaScript code to the page.
Page.RegisterStartupScript("FocusScript", focusScript)
End Sub
----
Gerry Roston
gerry@pairofdocs.net