简体   繁体   中英

How to hide ImageButton based on Row Cell value

I am trying to hide my imagebutton based on the cell value of another column.

So if my cell value.Text = "OPEN" then I want that specific imagebutton for that row to be invisible.

However my code hides all of the imagebuttons and I just wanna hide the ones that contain the cell text "OPEN"

Here is the code I have:

<asp:GridView ID="gvv" OnRowDataBound="gv1_RowDataBound" onrowcommand="gridupdate_RowCommand"  OnPreRender="GridView1_PreRender" class="table table-striped table-bordered table-hover" runat="server">
   <Columns>
   <asp:TemplateField HeaderStyle-Width ="115px" HeaderText="Action">
    <ItemTemplate>
<asp:ImageButton ID="ImageButton3" runat="server"  CommandName="Submit" ImageUrl="~/img/Sumbit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CommandName="ASN" ImageUrl="~/img/ASN-send.png" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/img/invoice.png" CommandName="View" />
&nbsp;
</ItemTemplate>
<HeaderStyle Width="115px"></HeaderStyle>
       </asp:TemplateField>
           </Columns>
            </asp:GridView>

Backend Code:

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.Cells(2).Text.ToString = "OPEN") Then

            Else
                Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
                imgBtn.Visible = False

            End If
        End If

    End Sub

You can use the following scenario if you are using telerik rad grid. hope that this may help you to find a solution.

 Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
        If e.Item.ItemType = GridItemType.AlternatingItem Or e.Item.ItemType = GridItemType.Item Then
            Dim imgBtn As ImageButton = DirectCast(e.Item.FindControl("ImageButton3"), ImageButton)
            If (e.Item.Cells(2).Text.ToString = "OPEN") Then
                imgBtn.Visible = True
            Else
                imgBtn.Visible = False
            End If
        End If
End Sub

I think your code works correctly but you just need to revise your If statement, it should be:

Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            If (e.Row.Cells(2).Text.ToString = "OPEN") Then
               'Hide ImageButton3
               Dim imgBtn As ImageButton = CType(e.Row.FindControl("ImageButton3"), ImageButton)
               imgBtn.Visible = False
            Else
                'Do nothing
            End If
        End If
    End Sub

Tried it on my side and it's working, unless you are doing something else in GridView1_PreRender method that maybe affect on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM