ASP.net - 将焦点设置为编辑行

发布于 2024-08-14 16:17:32 字数 805 浏览 6 评论 0原文

单击编辑按钮后,如何将焦点设置到 datagridview 行中的特定控件?当网格绑定时,我可以对新行执行此操作,但不能对现有行执行此操作。该控件似乎还不存在。

'这不起作用(现有行)

Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
        Try
            gvDays.EditIndex = e.NewEditIndex
            gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
        Catch ex As Exception
            Helper.WriteException(ex)
        End Try
    End Sub

'这对新绑定的行有效

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If e.Row.RowState = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
    End Sub

How do you set focus to a specific control in a datagridview row after clicking the edit button? I'm able to do it for a new row when the grid is binding, but not for an existing row. The control doesn't seem to exist yet.

'This doesn't work (existing row)

Protected Sub gvDays_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvDays.RowEditing
        Try
            gvDays.EditIndex = e.NewEditIndex
            gvDays.Rows(e.NewEditIndex).FindControl("txtDayText").Focus()
        Catch ex As Exception
            Helper.WriteException(ex)
        End Try
    End Sub

'This does work for a newly bound row

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If e.Row.RowState = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-08-21 16:17:32

gvDays_RowDataBound 应该可以工作,问题是您正在使用 = 运算符查看 e.Row.RowState,但 RowState 是一个位标志,

试试这个

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
End Sub

gvDays_RowDataBound should work, the problem is you are looking at e.Row.RowState using the = operator, but RowState is a bitflag

try this

Private Sub gvDays_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDays.RowDataBound
        If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit Then
            e.Row.Cells(3).Controls(0).Focus()
        End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文