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

标签 c# asp.net javascript html

我试过了,但是打不通:-

代码隐藏

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 (在这种情况下它应该出错)或者因为您当前的代码在某种模板中(在 GridViewRepeater 中)。后者是最有可能的,因为您使用数据语句 ( <%# ),它通常放置在数据绑定(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/

相关文章:

c# - FluentValidation:仅验证已更改的属性

javascript - 错误: "Maximum call stack size exceed" AJAX calling WebMethod

c# - 身份验证过滤器 : How to do a HTTP 401 with message

c# - 在 GridView 数据集中格式化 DateTime 字符串表示

javascript - 使用javascript将上传的文件分成多个 block

javascript - 如何测试指令链接中定义的 $on 事件

c# - Azure 存储连接错误(如果系统时间存在差异)

c# - ASP.NET MVC5 未为请求的 URL 配置网络服务器,且服务器上未启用目录浏览

c# - 灵活使用对象包装器类

javascript - 根据当前 URL 中的字符串修改源