开发者

Can I insert multiple <tbody> elements into an asp:Table programmatically?

开发者 https://www.devze.com 2023-03-03 13:53 出处:网络
See also the related question can we have multiple <tbody> in same <table>? Say I want to generate the following table using asp:table:

See also the related question can we have multiple <tbody> in same <table>?

Say I want to generate the following table using 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>
    <开发者_StackOverflow社区/tbody>
</table>

Can I build these <tbody> / <tr> elements programmatically?

I tried doing something like:

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);

But this just gives me the error 'Table' cannot have children of type 'HtmlGenericControl'.

I can find a TableRow, TableCell, TableHeaderRow, TableHeaderCell, etc. classes, but I can't seem to find TableBody or TableHeader. I can place rows within the header, body or footer:

descriptionRow.TableSection = TableRowSection.TableBody;

...but I can't seem to divide the rows into multiple different <tbody> elements. I've pretty much given up and gone to using an <asp:ListView> to generate the table, but I'm interested to see if this can be done.


There is no way to enhance asp:Table functionality for <tbody> just with its properties. But there are three ways I know how you can implement this:

  1. Create a class derived from System.Web.UI.WebControls.Table and override its method RenderContents with your own logic. Look a default implementation of .NET Framework native method (Table.RenderContents) to see how you can manage it by using any disassembler like .NET Reflector.

  2. (the hardest way) Create your own asp:Table with subclasses (TableBody, TableBodyRow, TableBodyCell, etc.) ;)

  3. Use asp:Repeater instead, like that:

<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>

or even with sub repeater (may be the better way if you have two collections -- customers and values by month)

P.S.: By looking your example, <th> inside <tbody> is incorrect in terms of HTML rules. Don't use it there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号