【问题标题】:gridview with 4 textboxes in a row in VB.NetVB.Net中连续4个文本框的gridview
【发布时间】:2015-10-10 12:44:31
【问题描述】:

我有一个网格视图,其中连续 4 个文本框。用户在第一行的第一个文本框中输入文本。其他文本框是只读的,它们的值会根据第一个文本框的值自动填充。当我按“enter”或“tab”键时,文本框焦点应该转到第二行的第一个文本框。我怎样才能做到这一点?.Tab 索引似乎不起作用。我是在 VB.Net 中做的

【问题讨论】:

    标签: javascript jquery asp.net vb.net


    【解决方案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,这样它们就永远不会被聚焦。

    文本框更改事件:-

    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 的文本框,并将焦点设置在该文本框上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2020-04-07
      • 2016-06-03
      • 2021-04-03
      • 2012-10-24
      相关资源
      最近更新 更多