如何根据行单元格值隐藏ImageButton

时间:2015-01-24 05:11:52

标签: asp.net vb.net gridview

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

所以,如果我的单元格值.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

2 个答案:

答案 0 :(得分:0)

如果您使用的是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

答案 1 :(得分:0)

我认为你的代码工作正常,但你只需要修改你的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方法中做了其他可能会对此产生影响的事情。