C# 如何确定HtmlTableCell控件是th还是td?

C# 如何确定HtmlTableCell控件是th还是td?,c#,asp.net,html-table,controls,C#,Asp.net,Html Table,Controls,我正在遍历我创建的一个表,并从每行中获取第一个单元格。根据细胞的类型,细胞会做不同的事情。例如: if(cCell.type=='th' && cCell.parent.next.Cells[0].type == 'th'){ cCell.parent.parent.controls.remove(cCell.parent); } 声明,如果当前单元格及其下的单元格都是th,则删除当前单元格行。没有HtmlTableHeaderCell控件,但在页面的html中成为,而它是

我正在遍历我创建的一个表,并从每行中获取第一个单元格。根据细胞的类型,细胞会做不同的事情。例如:

if(cCell.type=='th' && cCell.parent.next.Cells[0].type == 'th'){
  cCell.parent.parent.controls.remove(cCell.parent);
}

声明,如果当前单元格及其下的单元格都是th,则删除当前单元格行。

没有HtmlTableHeaderCell控件,但在页面的html中成为
,而它是
。我不完全理解您的问题的背景,但您应该能够使用typeof()区分TableCell和TableHeaderCell。所有HTMLControl都确认了
之间没有类型差异,两者都是HtmlTableCell


我认为,如果您想以编程方式生成表,最好使用Web控件,但如果您已经创建了表,则使用标记名本身并没有什么问题。

Tom Ingram似乎是正确的

    foreach (HtmlTableRow row in MyTable.Rows)
    {
        if (row.Cells[0].TagName.ToLower() == "th")
        {
            // header.
        }
        else
        {
            // cell.
        }
    }
使用的示例表

<table runat="server" id="MyTable">
    <tr>
        <th>header 1</th>
        <th>header 2</th>
    </tr>
    <tr>
        <td>cell 1</td>
        <td>cell 2</td>
    </tr>
</table>

标题1
标题2
第1单元
第2单元

如果要检查的表格单元格类型为System.Web.UI.WebControl.TableCell,则应该能够使用TagKey属性(

那么您的代码将是:

if(cCell.TagKey == HtmlTextWriterTag.Td && cCell.parent.next.Cells[0].TagKey == HtmlTextWriterTag.Th){
  cCell.parent.parent.controls.remove(cCell.parent);
}
虽然我不确定你想要完成什么,但我打赌有更简单的方法