C# 关于asp:表控件我们如何创建一个thead?

C# 关于asp:表控件我们如何创建一个thead?,c#,syntax,html-table,tablesorter,asp.net-3.5,C#,Syntax,Html Table,Tablesorter,Asp.net 3.5,从对主题的分析中,我们可以看到我们创建了一个TableHeaderRow,其中包含TableHeaderCells 但是他们添加了如下表头: myTable.Row.AddAt(0, headerRow); 其中输出HTML: <table id="Table1" ... > <tr> <th scope="column" abbr="Col 1 Head">Column 1 Header</th> <th scope=

从对主题的分析中,我们可以看到我们创建了一个
TableHeaderRow
,其中包含
TableHeaderCell
s

但是他们添加了如下表头:

myTable.Row.AddAt(0, headerRow);
其中输出HTML:

<table id="Table1" ... > 
<tr> 
    <th scope="column" abbr="Col 1 Head">Column 1 Header</th>
    <th scope="column" abbr="Col 2 Head">Column 2 Header</th>
    <th scope="column" abbr="Col 3 Head">Column 3 Header</th> 
</tr>
<tr> 
    <td>(0,0)</td>
    <td>(0,1)</td>
    <td>(0,2)</td>
</tr>

...

但是问题是关于
控件。

我本想建议您尝试声明性语法,但现在我知道您的意思了;以下代码…:

<asp:Table runat="server" ID="sometable">
    <asp:TableHeaderRow BackColor="#ADD9E6">
        <asp:TableHeaderCell ID="DataHeader" CssClass="Heading1" Text="Data Header" />
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell>Data</asp:TableCell>
    </asp:TableRow>
</asp:Table>

如果在模板中命名列时可以使用DataGrid,它将适当地生成一个thead和tbody。

刚刚找到了一种方法,我们确实需要使用从基控件继承的控件,例如

public class myTable : System.Web.UI.WebControls.Table
{
    protected override void OnPreRender(EventArgs e)
    {
        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            // first row is the Table Header <thead>
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            // last row is the Footer Header <tfoot> (comment for not using this)
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;

            FieldInfo field = typeof(WebControl).GetField("tagKey", BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (TableCell cell in table.Rows[0].Cells)
                field.SetValue(cell, HtmlTextWriterTag.Th);
        }
        base.OnPreRender(e);
    }
}
例如,您只需要使用它来代替基本控件:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        myGridView dg = new myGridView();
        dg.DataSource = new string[] { "1", "2", "3", "4", "5", "6" };
        dg.DataBind();

        ph.Controls.Add(dg);
    }
}
在aspx页面中,只需添加一个占位符,如:

<asp:PlaceHolder ID="ph" runat="server" />


请注意:这是VB.net对同一问题的回答,您可以使用任意数量的免费在线转换器将其转换为C#

使用thead&tbody创建一个简单的HTML表。我特别需要jQuery tablesorter插件的thead元素

    Dim ta As Table
    Dim thr As TableHeaderRow
    Dim thc As TableHeaderCell
    Dim tr As TableRow
    Dim td As TableCell

    ta = New Table
    ' make a header row & explicitly place it into the header section
    thr = New TableHeaderRow
    thr.TableSection = TableRowSection.TableHeader
    ' add cells to header row
    thc = New TableHeaderCell
    thc.Text = "ID"
    thr.Cells.Add(thc)
    thc = New TableHeaderCell
    thc.Text = "DESC"
    thr.Cells.Add(thc)
    ' create a data row & append cells to it
    tr = New TableRow
    td = New TableCell
    td.Text = "101"
    tr.Cells.Add(td)
    td = New TableCell
    td.Text = "VB.net is not so bad."
    tr.Cells.Add(td)
    ' append the header & row to the table
    ta.Rows.Add(thr)
    ta.Rows.Add(tr)

    ' add the new table to your page
    Page.Controls.Add(ta)

在这件事上我已经晚了一百万年,但我只需要做同样的事。它的处理方式与栅格视图基本相同

        foreach (var item in dynamicData)
        {
            var c = new TableHeaderCell()
            c.Text = item.Whatever;
            hr.Cells.Add(c);
        }
        //This is the important line
        hr.TableSection = TableRowSection.TableHeader;

        MyTable.Rows.Add(hr);
TableSection=“TableHeader”
添加到TableHeaderRow,如下所示:

<asp:TableHeaderRow ID="TableHeaderRow2" 
 runat="server" TableSection="TableHeader" >


我不能使用声明性语法:(这是一个动态表,只有在运行时我才知道有多少列是关于什么的……这个想法使用一个由设计师生成的XML来创建“网格”DataGrid默认情况下不这样做您不知道,如果您有数据源,为什么我要手工做所有事情?如果我可以简单地做
myTable.DataSource=myDataSource;
,那么计算列数、行数等,我只想创建
,因为其他一切都已经存在!
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        myGridView dg = new myGridView();
        dg.DataSource = new string[] { "1", "2", "3", "4", "5", "6" };
        dg.DataBind();

        ph.Controls.Add(dg);
    }
}
<asp:PlaceHolder ID="ph" runat="server" />
    Dim ta As Table
    Dim thr As TableHeaderRow
    Dim thc As TableHeaderCell
    Dim tr As TableRow
    Dim td As TableCell

    ta = New Table
    ' make a header row & explicitly place it into the header section
    thr = New TableHeaderRow
    thr.TableSection = TableRowSection.TableHeader
    ' add cells to header row
    thc = New TableHeaderCell
    thc.Text = "ID"
    thr.Cells.Add(thc)
    thc = New TableHeaderCell
    thc.Text = "DESC"
    thr.Cells.Add(thc)
    ' create a data row & append cells to it
    tr = New TableRow
    td = New TableCell
    td.Text = "101"
    tr.Cells.Add(td)
    td = New TableCell
    td.Text = "VB.net is not so bad."
    tr.Cells.Add(td)
    ' append the header & row to the table
    ta.Rows.Add(thr)
    ta.Rows.Add(tr)

    ' add the new table to your page
    Page.Controls.Add(ta)
        foreach (var item in dynamicData)
        {
            var c = new TableHeaderCell()
            c.Text = item.Whatever;
            hr.Cells.Add(c);
        }
        //This is the important line
        hr.TableSection = TableRowSection.TableHeader;

        MyTable.Rows.Add(hr);
<asp:TableHeaderRow ID="TableHeaderRow2" 
 runat="server" TableSection="TableHeader" >