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

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


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

asp:Table does not support these elements.

Update: As jameh’s answer reveals, the sentence above is completely wrong: the TableSection property allows to control
whether a given row goes into the table’s header, body or footer.

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

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

Original, now moot answer follows:

You might want to try the HtmlTable class instead:

<table id="yourId" runat="server">

Method 2

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.

UseAccessibleHeader is 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.

Here’s my example asp:Table markup:

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

At Page_Load, we specify that our TableHeaderRow1 should be a 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:
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"



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

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x