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

时间:2018-12-30 11:14:30

标签: asp.net vb.net webforms aspxgridview

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

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

要点如下。

  • 在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