【问题标题】:Change the visibility property from code behind从后面的代码更改可见性属性
【发布时间】:2016-09-20 19:36:41
【问题描述】:

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

【问题讨论】:

  • 您可能希望在您的 asp 问题中添加 asp/asp.net 标记以吸引 asp 的注意
  • @Plutonix 刚刚为他做了。
  • 为什么不能在 HTML 中添加 Visible 属性?
  • 可见属性是布尔值而不是字符串...更改此tb.Visible = "False" to tb.Visible = False
  • Dim tb As Button = e.Row.Cells(1).Controls(1) 抛出此错误:指定的参数超出了有效值的范围。参数名称:索引

标签: asp.net vb.net


【解决方案1】:

感谢所有的想法。这是我在这个网站上找到的最干净的解决方案,但它是用 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

【讨论】:

    【解决方案2】:

    也许你可以改成 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
    

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

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2014-10-13
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多