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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14169636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:56:49  来源:igfitidea点击:

Adding row to an existing html table from code behind

c#asp.netvisual-studio-2012

提问by Adnan Al-Husain

more of newbies :)

更多的新手:)

I've created HTML table using below html code

我已经使用下面的 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>

I tried to add row for it using

我尝试使用添加行

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);
}

but I got :

但我得到了:

The best overloaded method match for 'System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)' has some invalid arguments

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

I googled around but with no luck.

我用谷歌搜索,但没有运气。

I'm doing this to avoid Columns Heading merging action in code behind while I still have to look for how to merge rows heading. This is the last step for completing https://stackoverflow.com/questions/13670384/asp-net-dynamic-input-display-table-by-section-with-multi-columns-rows-headers

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

,.. closer heading view is:

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

enter image description here

在此处输入图片说明

Rows created will have a forwarding heading cell that should be merged if it's the same with row above. Appreaciate the great assistant from StackOverflow members.

创建的行将有一个转发标题单元格,如果它与上面的行相同,则应合并该单元格。感谢 StackOverflow 成员的好助手。

采纳答案by shahkalpesh

The error is apparent in the exception.

该错误在异常中很明显。

You are using TableRowinstead of HtmlTableRow.
Hover over TableRow tRow = new TableRow();to see which namespace is the TableRowclass from.

您正在使用TableRow而不是HtmlTableRow.
将鼠标悬停在上面TableRow tRow = new TableRow();以查看TableRow该类来自哪个命名空间。

A similar change for cell would be required. i.e. use HtmlTableCellinstead of TableCell.

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

EDIT: Table, TableRoware classes from System.Web.UI.WebControlsnamespace.
Whereas, HtmlTable, HtmlTableRoware classes from System.Web.UI.HtmlControlsnamespace.

编辑:TableTableRow是来自System.Web.UI.WebControls命名空间的类。
然而,HtmlTableHtmlTableRow是从类System.Web.UI.HtmlControls的命名空间。

回答by hojjat.mi

You just need more attention. You can try this:

你只是需要更多的关注。你可以试试这个:

Step 1:

第1步:

<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>

Step 2 : in code behind use this code:

第 2 步:在后面的代码中使用此代码:

 HtmlTableRow tRow = new HtmlTableRow();
 for (int i = 1; i < 3; i++)
 {
    HtmlTableCell tb = new HtmlTableCell();               
    tb.InnerText = "text";
    tRow.Controls.Add(tb);
 }
 Table1.Rows.Add(tRow);

Notice: Use this usingin code behind:

注意:using在后面的代码中使用它:

using System.Web.UI.HtmlControls;