HOW TO HIDE IMAGEBUTTON BASED ON ROW CELL VALUE?

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
C#  ASP.Net 
 1128  View(s)
Ratings:
Rate this:

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

  1. Simons Hood

    Post:70

    Points:494
    Re: How to hide ImageButton based on Row Cell value?

    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.

      Modified On Apr-09-2018 04:20:41 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!