0

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?

hjh93
  • 570
  • 11
  • 27

1 Answers1

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
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Hmmm..... Still not triggering. Do I need to call the RowDataBound Event into my Page_Load event for it to even trigger? If so, how do I do that? Simply telling the Page_Load to `call GridView1_RowDataBound()` is so not going to work based on my experience. – hjh93 Oct 12 '20 at 00:02
  • Ok, managed to figure it out. Forgot to set OnRowDataBound properties in my GridView ASP settings. Once that's done, it triggers no problem. Thanks! – hjh93 Oct 12 '20 at 00:23