【问题标题】:Remove <tr> tag dynamically by ID in C#在 C# 中通过 ID 动态删除 <tr> 标签
【发布时间】:2018-05-14 18:57:24
【问题描述】:

我有一个问题,我有这张表,里面有一堆信息,它们在线条等下面有 3 张图像,它们取决于某些条件等。代码如下:

 <table id="Table1" cellspacing="1" cellpadding="1" width="661" border="0" class="auto-style3">

  <tr>

    <td align="center"><img alt="" src="../images" id="sign1" runat="server" style="height:80px" />
      <asp:Label ID="lblUnderline1" runat="server" Text="____________________________"></asp:Label>
    </td>
    <td align="center"><img alt="" src="../images/" id="sign2" runat="server" style="height:80px" />
      <asp:Label ID="lblUnderline2" runat="server" Text="____________________________"></asp:Label>

    </td>
  </tr>
  <tr>
    <td align="center" runat="server" id="Extra"><img alt="" src="../images" style="height:80px" id="sign3" runat="server" /><br />
      <asp:Label ID="lblUnderline3" runat="server" Text="____________________________"></asp:Label>
    </td>
  </tr>
  <tr>
    <td align="center">
      <asp:Label ID="lblTitle1" runat="server" Text="Vice President"></asp:Label>
    </td>
    <td align="center">
      <asp:Label ID="lblTitle2" runat="server" Text=" Registrar"></asp:Label>
    </td>
  </tr>
</table>

看起来像这样:

如果您发现 Sign1 与顶部的线之间存在很大的差距,那么它假设就在下方。隐藏的第三张图片只有在 if 语句显示 If(This condition){sign3.visible =true 时才会出现但是行和标题之间的差距并没有消失我试过这个:

// Extra is the <tr> ID
     Extra.Visible = false;                     Extra.Style.Add("display", "none");
                    Extra.Style.Add("width", "0px");

那么有没有办法在诸如

之类的 if 条件下从 c# 中将整个标签从 HTML 中删除
    If(Condition This)
{
// Remove the <tr> Tag along the image in it 
}

【问题讨论】:

  • 我的建议是向您要隐藏的 TR 添加一个独特的类,然后在您的 css 文件中为该 TR 添加样式。现在,看起来 TR 默认的高度为 80 像素,您永远不会删除它。如果您不想创建/修改 css 样式表,则需要添加 Extra.Style.Add("height","0px");
  • 我添加了你提到的高样式也不起作用,然后我删除了默认的 80px 它不起作用

标签: c# css asp.net html-table


【解决方案1】:

因此,当我在这里查看您的代码时,您似乎正试图隐藏 <tr>' yeah that worked it hides the <td id=Extra but it doesnt hide the <tr 内的 td,因此请尝试隐藏 tr>

<tr runat="server" id="Extra" >
<td align="center" ><img alt="" src="../images" style="height:80px" id="sign3" runat="server" /><br />
  <asp:Label ID="lblUnderline3" runat="server" Text="____________________________"></asp:Label>
</td>

这将适用于您当前的动态条件

    Extra.Visible = false;               
  Extra.Style.Add("display", "none");
       Extra.Style.Add("width", "0px");Extra.Style.Add("height", "0px"); // Even this as your default height is 80px

【讨论】:

  • 哦,是的,这就是问题所在,现在它删除了 以及其中的所有内容!谢谢
猜你喜欢
  • 2018-05-03
  • 1970-01-01
  • 2021-12-25
  • 2011-08-26
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多