简体   繁体   English

如何基于行单元格值隐藏ImageButton

[英]How to hide ImageButton based on Row Cell value

I am trying to hide my imagebutton based on the cell value of another column. 我试图根据另一列的单元格值隐藏我的imagebutton。

So if my cell value.Text = "OPEN" then I want that specific imagebutton for that row to be invisible. 因此,如果我的单元格value.Text =“ OPEN”,那么我希望该行的特定图像按钮不可见。

However my code hides all of the imagebuttons and I just wanna hide the ones that contain the cell text "OPEN" 但是我的代码隐藏了所有图像按钮,我只想隐藏包含单元格文本“ 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. 如果使用的是telerik rad网格,则可以使用以下方案。 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: 我认为您的代码可以正常工作,但是您只需要修改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

Tried it on my side and it's working, unless you are doing something else in GridView1_PreRender method that maybe affect on this. 除非您在GridView1_PreRender方法中执行可能会对此造成影响的其他操作,否则请尝试一下并使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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