0

I use jquery DataTable in my MVC3 project. The table is dynamically generated through code behind. The only thing (so far) that I have troubles with, is the tbody. I want to add a tbody section with ID, without any rows, from code behind.

I've been looking a lot, and the best thing I got is:

TableRow tb = new TableRow();
tb.TableSection = TableRowSection.TableBody;
tb.ID = "Body";

But that way, I get a row in the tbody section, with one row with the id.. What I want to get is:

<tbody id="Body"></tbody>

How can I get that result from code behind?

Thanks

4

2 回答 2

1

Such thing is not possible with the generic Table control, it simply doesn't support giving ID to its <tbody>.

What you can do though is assign the desired ID as custom attribute of the table:

Table1.Attributes["data-tbodyid"] = "Body";

Then using jQuery assign this on the fly to the table's <tbody>:

$(document).ready(function () {
    $("table").each(function () {
        var tbodyId = $(this).data("tbodyid");
        if (tbodyId && tbodyId.length > 0)
            $(this).find("tbody").eq(0).attr("id", tbodyId);
    });
});
于 2013-08-04T14:58:13.600 回答
0

I ended up using HtmlGenericControl:

HtmlGenericControl tb = new HtmlGenericControl("tbody");
tb.ID = grid.GridBodyName;

Of course, the table itself, the header, the footer and its cells should also be HtmlGenericControl. Adding the body to the table, and the cells to the header\footer should be done that way:

HTMLTable.Controls.Add(tb);
于 2013-08-05T07:04:35.847 回答