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

时间:2009-12-14 18:33:42

标签: asp.net vb.net setfocus

如何在单击编辑按钮后将焦点设置到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

1 个答案:

答案 0 :(得分:4)

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