ASP.NET 2.0 - 带有tbody/thead的DataGrid

Ady*_*Ady 6 asp.net datagrid

有没有办法让DataGrid控件呈现tbody和thead HTML元素?

Ale*_*lex 9

虽然我喜欢"user186197"的答案,但该博客文章使用反射,但在非完全信任的托管环境中可能会出错.这是我们使用的,没有黑客:

public class THeadDataGrid : System.Web.UI.WebControls.DataGrid
{
    protected override void OnPreRender(EventArgs e)
    {
        this.UseAccessibleHeader = true; //to make sure we render TH, not TD

        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;
        }

        base.OnPreRender(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然这个答案有效,但我非常感激,这种东西让我对网络形式感到疯狂,并最终将我推向了MVC. (2认同)

Mik*_*liy 0

DataGrid 没有内置的东西来满足您的需求。看一下ASP.NET 2.0 CSS 友好控制适配器 1.0,它们内置了对 DataView 的支持,但似乎您可以轻松地将这种想法应用于 DataGrid。