我有一个连续4个文本框的网格视图。用户在第一行的第一个文本框中输入文本。其他文本框是只读的,并根据第一个文本框的值自动填充其值。当我按下“输入”或“标签”键时,文本框焦点应转到第二行的第一个文本框。我怎样才能实现这一目标?.Tab索引似乎无法正常工作。我在VB.Net中这样做
答案 0 :(得分:1)
您可以使用RoWDataBound
事件来实现此目的: -
<强>更新强>
声明一个页面级变量来保存TabIndex: -
Dim index As Short = 0
RowDataBoundEvent: -
Protected Sub grdCustomer_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
index += 1
Dim txtID As TextBox = DirectCast(e.Row.FindControl("txtID"), TextBox)
txtID.TabIndex = index
//Other TextBox(s)
Dim txtName As TextBox = DirectCast(e.Row.FindControl("txtName"), TextBox)
Dim txtCity As TextBox = DirectCast(e.Row.FindControl("txtCity "), TextBox)
txtName.TabIndex = txtCity.TabIndex = -1 //Set TabIndex of Readonly textbox to -1
End If
End Sub
此处txtID
将是每行中第一个文本框的文本框ID,并将其他文本框TabIndex设置为-1
,以便它们永远不会被聚焦。
TextBox Change Event: -
Protected Sub txtID_TextChanged(sender As Object, e As EventArgs)
Dim txtID As TextBox = CType(sender, TextBox)
//Logic to populate other textbox.
Dim focusIndex As Short = CShort(txtID.TabIndex + 1)
Dim tabbedRow = grdCustomer.Rows.OfType(Of GridViewRow) _
.FirstOrDefault(Function(x) (CType(x.FindControl("txtID"), TextBox)).TabIndex = focusIndex)
If tabbedRow IsNot Nothing Then
tabbedRow.FindControl("txtID").Focus()
End If
End Sub
<强>逻辑:强>
由于我们按顺序在每行中设置了第一个文本框的TabIndex
,因此在textChanged事件中,我发现TabIndex的文本框等于当前文本框tabIndex + 1并将焦点设置在该文本框上。