【问题标题】:can't render thead or tfoot tags in asp.net无法在 asp.net 中呈现 thead 或 tfoot 标签
【发布时间】:2014-08-05 15:30:54
【问题描述】:

根据this post

当你创建一个 Table 对象时,包括 TableHeaderRow 和 TableHeaderCell 控件。 TableHeaderRow 的默认值和 TableFooterRow 控件使控件呈现 thead、tbody 和 tfoot 元素

我一直在尝试让 theadtfoot 标记渲染但没有成功。

我正在使用我发布的那个链接中提到的类,我只得到tbody 和一堆嵌套的tr 标签。我正在使用 VS2012 pro 和 .net 4.5。

我做错了什么?

public void BuildHTMLTable(DataTable dt)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter w = new HtmlTextWriter(sw);

    Table tbl = new Table();

    // create table header
    TableHeaderRow thr = new TableHeaderRow();
    foreach (DataColumn col in dt.Columns)
    {
        TableHeaderCell thc = new TableHeaderCell();
        thc.Text = col.Caption;
        thr.Cells.Add(thc);
    }
    tbl.Rows.AddAt(0, thr);

    // write out each data row
    foreach (DataRow row in dt.Rows)
    {
        TableRow tr = new TableRow();
        foreach (var value in row.ItemArray)
        {
            TableCell td = new TableCell();
            td.Text = value.ToString();
            tr.Controls.Add(td);
        }
        tbl.Rows.Add(tr);
    }

    // Create table footer
    TableFooterRow tfr = new TableFooterRow();
    foreach (DataColumn col in dt.Columns)
    {
        TableCell th = new TableCell();
        th.Text = col.Caption;
        tfr.Cells.Add(th);
    }
    tbl.Rows.Add(tfr);

    // Renter the table to html writer
    tbl.RenderControl(w);
    // copy html writer to a string variable
    tableString = sw.ToString();
}

下面呈现的 HTML

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    更改 TableHeaderRow 和 TableFooterRow 的 TableSection 值。

    public void BuildHTMLTable(DataTable dt)
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter w = new HtmlTextWriter(sw);
    
        Table tbl = new Table();
    
        // create table header
        TableHeaderRow thr = new TableHeaderRow();
        thr.TableSection = TableRowSection.TableHeader; // ADD THIS LINE
    
        foreach (DataColumn col in dt.Columns)
        {
            TableHeaderCell thc = new TableHeaderCell();
            thc.Text = col.Caption;
            thr.Cells.Add(thc);
        }
        tbl.Rows.AddAt(0, thr);
    
        // write out each data row
        foreach (DataRow row in dt.Rows)
        {
            TableRow tr = new TableRow();
            foreach (var value in row.ItemArray)
            {
                TableCell td = new TableCell();
                td.Text = value.ToString();
                tr.Controls.Add(td);
            }
            tbl.Rows.Add(tr);
        }
    
        // Create table footer
        TableFooterRow tfr = new TableFooterRow();
        tfr.TableSection = TableRowSection.TableFooter; // ADD THIS LINE
        foreach (DataColumn col in dt.Columns)
        {
            TableCell th = new TableCell();
            th.Text = col.Caption;
            tfr.Cells.Add(th);
        }
        tbl.Rows.Add(tfr);
    
        // Renter the table to html writer
        tbl.RenderControl(w);
        // copy html writer to a string variable
        tableString = sw.ToString();
    }
    

    【讨论】:

    • 查看更新的答案。请注意,我不确定为什么 TableHeaderRow 和 TableFooterRow 默认没有设置这个。
    • 谢谢。做到了。我同意我不确定为什么它不是默认值。你摇滚!
    猜你喜欢
    • 2019-04-06
    • 2022-01-23
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2017-03-31
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多