【问题标题】:How to modify the tag <tbody> using StringBuilder (ASP.NET C#)?如何使用 StringBuilder (ASP.NET C#) 修改标签 <tbody>?
【发布时间】:2017-11-01 18:37:04
【问题描述】:

我正在尝试使用 StringBuilder 插入表格,但我需要编辑一些 "tbody" 标签,但它只是忽略了我尝试的任何内容:

我试过没有成功:

sb.AppendLine("&lt;table&gt;&lt;tbody class=' any-class'&gt;");

它只是在 HTML 页面中创建一个没有任何类的 tbody

试图插入除 tr, th, td 之外的另一个标签:

sb.AppendLine("&lt;table&gt;&lt;div class=' any-class'&gt;");

在这种情况下,它会在主表之前创建一个带有标签的空表,换句话说,它只为标签创建一个表,然后创建一个包含我想要的内容的表。

更改 tbody 属性的唯一方法是将其直接放入 CSS:

`tbody {
            display: flex;
            flex-flow: column wrap;
            max-height: 1360px;
        }`

但是我不想将此更改应用于所有表,有什么我做错了或者我可以做些什么来修复它?

【问题讨论】:

  • 在创建字符串和浏览器渲染它之间必须有一个步骤。你是如何看待渲染输出的?您是否尝试过将 stringbuilder 的内容写入文件以检查它是否符合您的预期?
  • 你能把使用StringBuilder的完整方法贴出来吗?还有另一个因素导致您的问题。发布的代码不会给出您所描述的行为。它可能就像标记中遗漏的字符一样简单

标签: c# html css asp.net


【解决方案1】:

我建议在桌子上设置课程。然后你可以设置元素的类。请注意使用\" 来获取字符串中的双引号。

StringBuilder sb = new StringBuilder();

sb.AppendLine("<table class=\"any-class\">");
sb.AppendLine("<thead>");
sb.AppendLine("<tr>");
sb.AppendLine("<th>Name</th>");
sb.AppendLine("<th>Age</th>");
sb.AppendLine("</tr>");
sb.AppendLine("</thead>");
sb.AppendLine("<tfoot>");
sb.AppendLine("<tr>");
sb.AppendLine("<td>Total</td>");
sb.AppendLine("<td>2</td>");
sb.AppendLine("</tr>");
sb.AppendLine("</tfoot>");
sb.AppendLine("<tbody>");
sb.AppendLine("<tr>");
sb.AppendLine("<td>Jane</td>");
sb.AppendLine("<td>45</td>");
sb.AppendLine("</tr>");
sb.AppendLine("<tr>");
sb.AppendLine("<td>John</td>");
sb.AppendLine("<td>20</td>");
sb.AppendLine("</tr>");
sb.AppendLine("</tbody>");
sb.AppendLine("</table>");

Literal1.Text = sb.ToString();

现在table 具有any-class 类,您可以设置CSS。

<style>
    .any-class {
        border: 2px dotted red;
    }

        .any-class td {
            padding: 3px;
            color: blue;
        }

        .any-class tbody {
            display: flex;
            flex-flow: column wrap;
            max-height: 1360px;
        }

        .any-class tfoot {
            background-color: #c5c5c5;
            font-weight: bold;
        }
</style>

但是像这样建表效率不高。请注意,GridView 在 HTML 中也是 table。或者,您可以使用 Table table = new Table(); 在代码后面创建一个“真实”表

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 2016-05-20
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多