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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x