从后面的代码向现有的html表添加行

时间:2013-01-05 07:22:54

标签: c# asp.net visual-studio-2012

更多新手:)

我使用以下html代码

创建了HTML表格
 <table runat="server" id="Table1" border="1" class="style1">
    <tr>
        <td class="style2" colspan="3">
            Info.</td>
        <td class="style2" colspan="2">
            TypeA</td>
        <td class="style2">
            TypeB</td>
        <td class="style2" rowspan="2">
            TypeC</td>
    </tr>
    <tr>
        <td class="style3">
            Dept.</td>
        <td class="style3">
            Div.</td>
        <td class="style3">
            Unit</td>
        <td class="style3">
            TypeA.1</td>
        <td class="style3">
            TypeA.1</td>
        <td style="text-align: center">
            TypeB.1</td>
    </tr>
</table>

我尝试使用

添加行
protected void Button1_Click(object sender, EventArgs e)
{
    TableRow tRow = new TableRow();
    for (int i = 1; i < 8; i++)
    {
        TableCell tb = new TableCell();
        tb.Text = "text";
        tRow.Controls.Add(tb);
    }
    Table1.Rows.Add(tRow);
}

但我得到了:

  

'System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)'的最佳重载方法匹配有一些无效的参数

我用Google搜索但没有运气。

我这样做是为了避免Columns Heading在代码中合并操作,而我仍然需要查找如何合并行标题。这是完成https://stackoverflow.com/questions/13670384/asp-net-dynamic-input-display-table-by-section-with-multi-columns-rows-headers

的最后一步

,..更近的标题视图是:

enter image description here

创建的行将具有转发标题单元格,如果它与上面的行相同,则应合并。 Appreaciate StackOverflow成员的伟大助手。

1 个答案:

答案 0 :(得分:4)

错误在异常中很明显。

您使用TableRow代替HtmlTableRow 将鼠标悬停在TableRow tRow = new TableRow();上可查看哪个命名空间是TableRow类。

需要对细胞进行类似的更改。即使用HtmlTableCell代替TableCell

编辑:TableTableRow是来自System.Web.UI.WebControls命名空间的类 鉴于HtmlTableHtmlTableRow是来自System.Web.UI.HtmlControls命名空间的类。

相关问题