【问题标题】:Can I insert multiple <tbody> elements into an asp:Table programmatically?我可以以编程方式将多个 <tbody> 元素插入到 asp:Table 中吗?
【发布时间】:2011-05-06 05:03:09
【问题描述】:

另见相关问题can we have multiple <tbody> in same <table>?

假设我想使用 asp:table 生成下表:

<table>
    <thead>
        ...stuff...
    </thead>
    <tbody class="odd">
        <th scope="tbody">CUSTOMER 1</th>
        <tr><td>#1</td><td>January</td></tr>
        <tr><td>#2</td><td>April</td></tr>
        <tr><td>#3</td><td>March</td></tr>
    </tbody>
    <tbody class="even">
        <th scope="tbody">CUSTOMER 2</th>
        <tr><td>#1</td><td>January</td></tr>
        <tr><td>#2</td><td>April</td></tr>
        <tr><td>#3</td><td>March</td></tr>
    </tbody>
    <tbody class="odd">
        <th scope="tbody">CUSTOMER 3</th>
        <tr><td>#1</td><td>January</td></tr>
        <tr><td>#2</td><td>April</td></tr>
        <tr><td>#3</td><td>March</td></tr>
    </tbody>
</table>

我可以通过编程方式构建这些&lt;tbody&gt; / &lt;tr&gt; 元素吗?

我尝试做类似的事情:

var x = new HtmlGenericControl("tbody");
x.Attributes["class"] = jobNum % 2 == 0 ? "even" : "odd";
TableRow xinfoRow = buildXInfoRow(job); x.Controls.Add(xinfoRow);
TableRow xdescriptionRow = buildXDescriptionRow(job); x.Controls.Add(xdescriptionRow);
myTable.Controls.Add(x);

但这只是给了我错误'Table' cannot have children of type 'HtmlGenericControl'

我可以找到TableRow、TableCell、TableHeaderRow、TableHeaderCell等类,但我似乎找不到TableBody或TableHeader。我可以在页眉、正文或页脚中放置行:

descriptionRow.TableSection = TableRowSection.TableBody;

...但我似乎无法将行划分为多个不同的 &lt;tbody&gt; 元素。我几乎已经放弃并转而使用&lt;asp:ListView&gt; 来生成表格,但我很想看看这是否可以完成。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    没有办法仅通过属性来增强&lt;tbody&gt;asp:Table 功能。但我知道您可以通过三种方式来实现它:

    1. 创建一个派生自System.Web.UI.WebControls.Table 的类,并用您自己的逻辑覆盖其方法RenderContents。查看 .NET Framework 本地方法 (Table.RenderContents) 的默认实现,了解如何使用 .NET Reflector 等反汇编程序对其进行管理。

    2. (最难的方法)使用子类(TableBody、TableBodyRow、TableBodyCell 等)创建自己的asp:Table ;)

    3. 改用asp:Repeater,像这样:

    <asp:Repeater ...>
      <HeaderTemplate>
        <table>
          <thead>
            ..stuff..
          </thead>
          <tbody class="odd">
      </HeaderTemplate>
      <ItemTemplate>
        <tr> ..stuff.. </tr>
      </ItemTemplate>
      <SeparatorTemplate>
        <asp:PlaceHolder Visible='<%# (Container.ItemIndex + 1) % 3 %>' runat="server">
           <tbody class="<%# (Container.ItemIndex + 1) % 6 > 3 ? "even" : "odd" %>">
        </asp:PlaceHolder>
      </SeparatorTemplate>
      <FooterTemplate>
          </tbody>
        </table>
      </FooterTemplate>
    </asp:Repeater>
    

    甚至使用子中继器(如果您有两个集合——客户和按月计算的值,这可能是更好的方法)

    P.S.:通过查看您的示例,&lt;tbody&gt; 中的 &lt;th&gt; 在 HTML 规则方面是不正确的。不要在那里使用它。

    【讨论】:

    • 如果您不是绝对必须使用 asp:table 控件,您当然可以使用 HTMLGenericControl 生成标记。例如var tbl = new HtmlGenericControl("table");var tHead = new HTMLGenericControl("thead");
    猜你喜欢
    • 2013-02-17
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2011-05-01
    相关资源
    最近更新 更多