2009-12-14 99 views
1

如何設置聚焦到特定的控制在一個DataGridView行單擊編輯按鈕後?當網格綁定時,我可以爲它做一個新行,但不能用於現有的行。控制似乎還沒有存在。ASP.net - 將焦點設置到編輯行

'這行不通(現有行)

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 

回答

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