Home > DeveloperSection > Forums > How to hide ImageButton based on Row Cell value?
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    April-12-2015 11:44 PM

 .NET C#  ASP.Net 
Ratings:


 1 Reply(s)
 556  View(s)
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



Simons Hood
Simons Hood

Total Post:70

Points:494
Supported
Posted on    April-13-2015 12:08 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick