如何根据 Row Cell 值隐藏 ImageButton

How to hide ImageButton based on Row Cell value

我试图根据另一列的单元格值隐藏我的图像按钮。

因此,如果我的单元格 value.Text = "OPEN" 那么我希望该行的特定图像按钮不可见。

但是我的代码隐藏了所有的图像按钮,我只想隐藏那些包含单元格文本的按钮 "OPEN"

这是我的代码:

<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>

后端代码:

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

如果您使用的是 telerik rad 网格,则可以使用以下方案。希望这可以帮助您找到解决方案。

 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

我认为你的代码工作正常,但你只需要修改你的 If 语句,它应该是:

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

在我这边试过了,它工作正常,除非你在 GridView1_PreRender 方法中做其他可能影响这个的事情。