我在 UpdatePanel 中有四个文本框,并且所有文本框的 AutoPostBack 都设置为 True。当用户输入一个值并按 Enter 键时,我希望光标自动移动到下一个文本框。当我在 UpdatePanel 中没有控件时,标准的 textbox.focus 方法可以正常工作。
我在这里找到了一些代码,导致我创建了这个:
Protected Sub txtField_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtField1.TextChanged,
txtField2.TextChanged, txtField3.TextChanged
'Based on the textbox where data was changed, move the cursor to the next field.
Try
Dim sm As ScriptManager = ScriptManager.GetCurrent(Me)
'As multiple textboxes fire this same event, determine which textbox triggered this.
Dim MyTextbox As TextBox = TryCast(sender, TextBox)
Select Case MyTextbox.ID.ToString
Case "txtField1"
sm.SetFocus(txtField2)
Case "txtField2"
sm.SetFocus(txtField3)
Case "txtField3"
sm.SetFocus(txtField4)
End Select
Catch ex As Exception
lblError.Text = "Error in [txtField_TextChanged]: " & ex.Message
End Try
End Sub
真正奇怪的是,这在我首先尝试的任何领域都有效。之后,事件被触发,但焦点没有改变。后续通话我还需要做些什么吗?
我将不胜感激任何建议。谢谢!