如何检查网格视图数据绑定事件中的行是否为真?

How to check rows are true in grid view databound event?

我有一个带有一个按钮的 Gridview,如果所有行都为真或选中,则按钮状态设置为启用,否则它将被禁用。 这是我的代码,但它对我不起作用。

If e.Row.RowType = DataControlRowType.DataRow Then


        Dim ckbox As CheckBox = CType(e.Row.FindControl("workfinished"), CheckBox)

        If ckbox.Checked = True Then
            btnFinishedAllWork.Enabled = True

        End If



    End If

您应该使用下面提到的示例代码来实现您的要求。

重点如下。

  • 始终在 Page_Load 事件
  • 的页面生命周期开始时将按钮的启用状态设置为 true
  • 然后,当绑定网格数据行时,发生在 Page_Load 事件之后,您可以在取消选中任何复选框时将按钮状态设置为禁用

VB.NET 下面的代码改变按钮状态

''In Page_Load event set the button to enabled state and then change its state later
''in page life cycle depending on if a check box is unchecked.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

btnFinishedAllWork.Enabled = True

End Sub



Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then
        Dim ckbox As CheckBox = CType(e.Row.FindControl("workfinished"), CheckBox)
        ''only change the state of button any one of the check boxes is unchecked
        If ckbox.Checked = False Then
            btnFinishedAllWork.Enabled = False
        End If
    End If

End Sub