簡體   English   中英

在VB.Net中連續有4個文本框的gridview

[英]gridview with 4 textboxes in a row in VB.Net

我有一個gridview連續有4個文本框。用戶在第一行的第一個文本框中輸入文本。 其他文本框是只讀的,其值將根據第一個文本框的值自動填充。 當我按Enter鍵或Tab鍵時,文本框焦點應移至第二行的第一個文本框。 我怎么能做到這一點?.Tab索引似乎不起作用。 我正在VB.Net中

您可以使用RoWDataBound事件來實現此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的文本框,並將焦點設置在該文本框上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM