如何将.NET TableCell呈现为TH而不是TD?

如何将.NET TableCell呈现为TH而不是TD?,.net,.net,我正在动态地构建一个.NET表,包括设置了TableSection的TableRows,结果是1-THEAD行和多个TBODY行。现在,我需要使用TH标记而不是TD标记来渲染THEAD行中的TableCell。我该怎么做?我还没有找到相应的TableCell属性,它不允许我向行单元格集合添加文本。您可以使用HtmlGenericControl th=new-HtmlGenericControl(“th”)并将其添加到thead行。您尝试过吗?另一种解决方案是继承TableCell类并覆盖Rend

我正在动态地构建一个.NET表,包括设置了TableSection的TableRows,结果是1-THEAD行和多个TBODY行。现在,我需要使用TH标记而不是TD标记来渲染THEAD行中的TableCell。我该怎么做?我还没有找到相应的TableCell属性,它不允许我向行单元格集合添加文本。

您可以使用
HtmlGenericControl th=new-HtmlGenericControl(“th”)
并将其添加到thead行。

您尝试过吗?

另一种解决方案是继承
TableCell
类并覆盖
Render
方法

这使您能够真正定制您的网络控制,并添加可能对您的特定场景有益的其他方法

protected override void Render(HtmlTextWriter writer)
    {
        if (Type == CellType.th)
        {
            writer.Write(HtmlTextWriter.TagLeftChar + "th"); // Render <th
            Attributes.Render(writer); // Render any associated attributes
            writer.Write(HtmlTextWriter.TagRightChar); // Render >
            base.RenderContents(writer); // Render the content between the <th></th> tags
            writer.Write(HtmlTextWriter.EndTagLeftChars + "th" + HtmlTextWriter.TagRightChar); // Render </th>
        }
        else
            base.Render(writer); // Defaults to rendering <td>
    }

如何将其添加到行中?使用row.Cells.Add(mycell)会产生编译时错误,而row.Controls.Add(mycell)会产生运行时错误。
public enum CellType
{
    td,
    th
}

private CellType _Type;
public CellType Type
{
    get { return _Type; }
    set { _Type = value; }
}