问题描述
另请参阅相关问题可以有多个< tbody>在同一< table>中?
说我想使用asp: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>
</tbody>
</table>
我可以以编程方式构建这些< tbody>
/< tr>
元素吗?
Can I build these <tbody>
/ <tr>
elements programmatically?
我尝试做类似的事情:
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'不能有'HtmlGenericControl'类型的子代
.
But this just gives me the error 'Table' cannot have children of type 'HtmlGenericControl'
.
我可以找到TableRow,TableCell,TableHeaderRow,TableHeaderCell等类,但是似乎找不到TableBody或TableHeader.我可以在页眉,正文或页脚中放置行:
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;
...但是我似乎无法将行分成多个不同的< tbody>
元素.我已经完全放弃了,开始使用< asp:ListView>
生成表,但是我很想知道是否可以做到这一点.
...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.
推荐答案
仅凭其属性,就无法增强< tbody>
的 asp:Table
功能.但是我知道有三种方法可以实现此目的:
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:
-
创建一个从
System.Web.UI.WebControls.Table
派生的类,并使用您自己的逻辑覆盖其方法RenderContents
.查看.NET Framework本机方法的默认实现(Table.RenderContents
),以了解如何使用任何反汇编程序(如.NET Reflector)来管理它.
Create a class derived from
System.Web.UI.WebControls.Table
and override its methodRenderContents
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.
(最困难的方法)创建带有子类(TableBody,TableBodyRow,TableBodyCell等)的自己的 asp:Table
;)
(the hardest way) Create your own asp:Table
with subclasses (TableBody, TableBodyRow, TableBodyCell, etc.) ;)
改为使用 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>
甚至使用子转发器(如果您有两个集合(按月列出的客户和价值)可能是更好的方法
or even with sub repeater (may be the better way if you have two collections -- customers and values by month)
P.S .:通过查看您的示例,就HTML规则而言,< tbody>
中的< th>
是不正确的.不要在那里使用它.
P.S.: By looking your example, <th>
inside <tbody>
is incorrect in terms of HTML rules. Don't use it there.
这篇关于我可以插入多个< tbody>元素转换为asp:Table的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!