jQuery 如何在 ASP.NET Table 中创建 thead 和 tbody?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4015839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:30:31  来源:igfitidea点击:

How to create thead and tbody in ASP.NET Table ?

jqueryasp.nethtml-table

提问by Primoz

How to create thead and tbody in ASP.NET Table? I need those tags because of jquery and asp.net gives me only tr, th and td.

如何在 ASP.NET 表中创建 thead 和 tbody?我需要这些标签,因为 jquery 和 asp.net 只给我 tr、th 和 td。

回答by Frédéric Hamidi

asp:Tabledoes not support these elements.

asp:Table不支持这些元素。

Update:As jameh's answerreveals, the sentence above is completely wrong: the TableSectionproperty allows to control whether a given row goes into the table's header, body or footer.

更新:正如jameh 的回答所揭示的,上面的句子是完全错误的:该TableSection属性允许控制给定的行是否进入表格的页眉、正文或页脚。

To elaborate on his answer, it seems you can even achieve this declaratively by setting the TableSectionproperty in your markup, without code behind:

为了详细说明他的答案,您似乎甚至可以通过TableSection在标记中设置属性来声明性地实现这一点,而无需背后的代码:

<asp:Table id="yourId" runat="server">
    <asp:TableHeaderRow TableSection="TableHeader">
        <!-- ... -->
    </asp:TableHeaderRow>
    <asp:TableRow>
        <!-- 'TableSection' defaults to 'TableRowSection.TableBody'. -->
        <!-- ... -->
    </asp:TableRow>
    <asp:TableRow TableSection="TableFooter">
        <!-- ... -->
    </asp:TableRow>
</asp:Table>


Original, now moot answer follows:

原始的,现在没有实际意义的答案如下:

You might want to try the HtmlTableclass instead:

您可能想尝试使用HtmlTable类:

<table id="yourId" runat="server">
    <thead>
        .
        .
        .
    </thead>
    <tbody>
        .
        .
        .
    </tbody>
</table>

回答by jfsaliba

Frédéric's answer is not accurate. asp:Table DOES in fact support <tbody>and <thead>tags, but in a less obvious fashion than HtmlTable.

弗雷德里克的回答不准确。asp:Table 实际上支持<tbody><thead>标签,但不像 HtmlTable 那样明显。

UseAccessibleHeaderis true by default for tables, which means your header rows will be rendered properly with <th>instead of <td>, but to get the <tbody>and <thead>tags, you've just got to set some voodoo at Page_Load and when you're creating/inserting your rows in the codebehind.

UseAccessibleHeader对于表格默认为true,这意味着您的标题行将使用<th>而不是正确呈现<td>,但要获取<tbody><thead>标签,您只需在 Page_Load 以及在创建/插入行时设置一些伏都教代码隐藏。

Here's my example asp:Table markup:

这是我的示例 asp:Table 标记:

<asp:Table runat="server" ID="tblGeneral">
    <asp:TableHeaderRow ID="TableHeaderRow1" runat="server">
        <asp:TableHeaderCell ID="TableHeaderCell1" runat="server">Column 1</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="TableHeaderCell2" runat="server">Column 2</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="TableHeaderCell3" runat="server">Column 3</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="TableHeaderCell4" runat="server">Column 4</asp:TableHeaderCell>
        <asp:TableHeaderCell ID="TableHeaderCell5" runat="server">Column 5</asp:TableHeaderCell>
    </asp:TableHeaderRow>
</asp:Table>

At Page_Load, we specify that our TableHeaderRow1 should be a TableHeader:

在 Page_Load,我们指定 TableHeaderRow1 应该是一个 TableHeader:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    TableHeaderRow1.TableSection = TableRowSection.TableHeader      
End Sub

And finally, in your function that inserts rows into said table, you just have to specify that the TableRowSection of each row you add is a TableBody:

最后,在将行插入所述表的函数中,您只需指定添加的每一行的 TableRowSection 是一个 TableBody:

Dim row As TableRow
Dim dvRow As Data.DataRowView

For Each dvRow In dv
    row = New TableRow
    row.TableSection = TableRowSection.TableBody 'THIS is the important bit
    cell = New TableCell
    Col1Stuff = New Label
    Col1Stuff.Text = "Blah"
    cell.Controls.Add(Col1Stuff)
    row.Cells.Add(cell)

    ...

tblGeneral.Rows.Add(row)
Next

You can do more reading on the TableRowSection property; looks like you can also accomplish this with your asp:Table template.

您可以对TableRowSection 属性进行更多阅读;看起来你也可以用你的 asp:Table 模板来完成这个。