簡體   English   中英

如何顯示/隱藏表格行 <tr> 在.ascx頁面

[英]How to show/hide table row <tr> in .ascx page

我嘗試了這個,但無法通過: -

代碼背后

protected HtmlTableRow trComment;

protected void Page_Load(object sender, EventArgs e)
{
    //Show/Hide table rows (TR)
    trComment.Visible = ConfigUtil.DisplaySummaryComment;
}

.ascx頁面

<tr id="trComment" runat="server">
    <td style="vertical-align:top; text-align:left;">
        <%#ConfigUtil.FieldLabels["PIComments"]%>
        :
    </td>
    <td>
        <%= Test.Comment %>
    </td>
</tr>

您的原始代碼不起作用,不是因為它不正確,而是因為您可能有更多地方使用trComment (在這種情況下它應該出錯)或者因為您當前的代碼位於某種模板中(在GridView ,在Repeater ) 。 后者很有可能,因為您使用的是數據語句( <%# ),它通常放在數據綁定控件模板中(但不一定)。

統一和輕松地解決這個問題的一種方法(存在許多方法,最好不要使用文字表)是使用asp:PlaceHolder ,它不會留下HTML“痕跡”,但可以用來切換任何HTML塊/ ASP.NET代碼:

<!-- toggle through OnLoad (can use ID as well) -->
<asp:PlaceHolder runat="server" OnLoad="MakeVisibleOrNot">
    <tr>
       ...
    </
</asp:PlaceHolder>

在代碼背后

protected void MakeVisibleOrNot(object sender, EventArgs e)
{
    ((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}
<tr id="trComment" runat="server">
   <td>

   </td>
</tr>

然后在您的Page_Load()方法中找到您的元素並將可見性設置為true或false,如下所示

protected void Page_Load(object sender, EventArgs e)
{
   trComment.Visible = false; //or trComment.Visible = true; as you wish
}

希望這對你有所幫助

嘗試

trComment.Style.Add("display", "none");

這也適用於沒有代碼

                        <asp:PlaceHolder runat="server" Visible ='<%# Convert.ToBoolean(Session["sess_isArtist"].ToString() == "1" || Session["sess_isBeneficiary"].ToString() == "1" ? "true": "false") %>'>
<tr>
   ...
</
                        </asp:PlaceHolder>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM