从后面的代码更改visibility属性

时间:2016-09-20 19:36:42

标签: asp.net vb.net

我试图隐藏“删除”'链接,如果CUST_ORDER_ID中的值=' X'但我不知道如何将可见性属性设置为" False"

我的asp。

<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ROWID" SortExpression="ROWID" Visible="False">         </asp:BoundField>
<asp:BoundField DataField="CUST_ORDER_ID" HeaderText="ORDER ID"     SortExpression="CUST_ORDER_ID">
<ItemStyle Width="50px"></ItemStyle>

    

和背后的代码

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        'check is row non order type and allow user to delete
        Dim oid As TableCell = e.Row.Cells(2)
        If oid.Text = "X" Then
            Dim tb As Button = e.Row.Cells(1).Controls(1)
            'Dim tb = e.Row.FindControl("DeleteButton")
            tb.Visible = "False"
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

感谢所有的想法。这是我在这个网站上找到的最干净的解决方案,但它在c#中转换为vb。

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton runat="server" ID="DeleteButton" CommandName="Delete" Text="Delete" />
    </ItemTemplate>
</asp:TemplateField>            

和背后的代码

Dim oid As TableCell = e.Row.Cells(2)
Dim tb = e.Row.FindControl("DeleteButton")
If oid.Text = "X" Then
    tb.Visible = True
Else
    tb.Visible = False
End If

答案 1 :(得分:-1)

也许您可以更改为Templatefield

<asp:TemplateField HeaderText="Col1">
   <ItemTemplate>
     <asp:label ID="lbl1" runat="server" text='<%#left(DataBinder.Eval(Container.DataItem, "field1"),20)%>'>
     </asp:label>
  </ItemTemplate>

使用此代码:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim lbl1 As Label
   If e.Row.RowType = DataControlRowType.DataRow Then
    lbl1 = CType(e.Row.FindControl("lbl1"), Label)
    If oid.Text = "X" Then
       lbl1 .Visible = "False"
    End If
   End If
End Sub

您几乎可以在模板中使用任何控件。