我试过了,但是打不通:-
代码隐藏
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
中)。后者是最有可能的,因为您使用数据语句 ( <%#
),它通常放置在数据绑定(bind)控件模板中(但不一定)。
统一而轻松地解决此问题的一种方法(存在许多方法,最好不要使用文字表)是使用 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;
}
关于c# - 如何在 .ascx 页面中显示/隐藏表行 <tr>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1701949/