如何在.ascx页面中显示/隐藏表行<tr>

Jan*_*ngo 7 html javascript c# asp.net

我尝试了这个,但无法通过: -

代码背后

protected HtmlTableRow trComment;

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

.ascx页面

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

Abe*_*bel 13

您的原始代码不起作用,不是因为它不正确,而是因为您可能有更多的地方trComment(在这种情况下它应该出错)或者因为您当前的代码位于某种模板中(在a GridView,a中Repeater).后者很有可能,因为你使用的是data-statement(<%#),它通常放在数据绑定控件模板中(但不一定).

统一和轻松地解决这个问题的一种方法(存在许多方法,最好不要使用文字表)是使用一个asp:PlaceHolder不会留下HTML"痕迹"的,但可以用来切换任何HTML/ASP块. NET代码:

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

在代码背后

protected void MakeVisibleOrNot(object sender, EventArgs e)
{
    ((Control) sender).Visible = ConfigUtil.DisplaySummaryComment;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

<tr id="trComment" runat="server">
   <td>

   </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

然后在您的 Page_Load() 方法中找到您的元素并将可见性设置为 true 或 false,如下所示

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

希望这对你有帮助