How to block command button in GridView when column data has specific value?

53 views Asked by At

I currently have a GridView with the following columns

| # |    Date   | Line | Process | Equipment | Step | Status |      |
---------------------------------------------------------------------
| 1 | 9/10/2020 |  A1  |   ABCD  |    SXCD   | Test |  Open  | Edit |

The last column is housing a command button for each row.

'ASP for button column for reference
<asp:TemplateField>
                          
            <ItemTemplate>
                <asp:linkbutton ID="Edit" runat="server" onclick="edit_click"
                commandbutton = "MySelect" commandargument = "<%# container.displayindex  %>"
                text="Edit"></asp:linkbutton>
            </ItemTemplate>

</asp:TemplateField>

I now plan to block it's function if the Status says Close.

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

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
        If StatusLog = "Close" Then

            Dim lb As LinkButton = DirectCast(e.Row.Cells(8).Controls(0), LinkButton)
            lb.Visible = False

        End If
    End If

End Sub

Here is the code I picked up. When testing, I find that the Edit button can still be interacted even if Status is Close.

What am I missing here?

1

There are 1 answers

2
Albert D. Kallal On BEST ANSWER

Hum, that code should work. However, as a general rule, it possbile you not picking up the control correctly.

I would suggest this code with a strong typed control, and using find control. its too difficult to risk the cells, and there can always be some kind of extra control or seperater etc. I find it best to use FindControl.

So, say like this:

If e.Row.RowType = DataControlRowType.DataRow Then
    Dim StatusLog As String = DataBinder.Eval(e.Row.DataItem, "Status").ToString()
    If StatusLog = "Close" Then

       Dim lb As LinkButton =  e.Row.FindControl("Edit")
       lb.Visible = False

    End If
End If