【问题标题】:How to show/hide table row <tr> in .ascx page如何在 .ascx 页面中显示/隐藏表格行 <tr>
【发布时间】:2010-12-14 16:32:28
【问题描述】:

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

后面的代码

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>

【问题讨论】:

    标签: c# asp.net javascript html


    【解决方案1】:

    试试

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

    【讨论】:

    • ASP.NET 中的.Visible 完全阻止了该控件的呈现。如果这不起作用,display:none; 也不起作用。
    【解决方案2】:

    您的原始代码不起作用,不是因为它不正确,而是因为您可能有更多带有trComment 的位置(在这种情况下它应该出错)或者因为您当前的代码在某种模板中(在GridViewRepeater)。后者最有可能,因为您使用数据语句 (&lt;%#),它通常放置在数据绑定控件模板中(但不一定)。

    统一且轻松地解决此问题的一种方法(存在多种方法,最好不要使用文字表)是使用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;
    }
    

    【讨论】:

      【解决方案3】:
      <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
      }
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        这也可以在没有代码的情况下使用

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多