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
Adding row to an existing html table from code behind
提问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:
,.. 更近的标题视图是:
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 TableRow
instead of HtmlTableRow
.
Hover over TableRow tRow = new TableRow();
to see which namespace is the TableRow
class from.
您正在使用TableRow
而不是HtmlTableRow
.
将鼠标悬停在上面TableRow tRow = new TableRow();
以查看TableRow
该类来自哪个命名空间。
A similar change for cell would be required. i.e. use HtmlTableCell
instead of TableCell
.
需要对单元进行类似的更改。即使用HtmlTableCell
代替TableCell
。
EDIT: Table
, TableRow
are classes from System.Web.UI.WebControls
namespace.
Whereas, HtmlTable
, HtmlTableRow
are classes from System.Web.UI.HtmlControls
namespace.
编辑:Table
,TableRow
是来自System.Web.UI.WebControls
命名空间的类。
然而,HtmlTable
,HtmlTableRow
是从类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 using
in code behind:
注意:using
在后面的代码中使用它:
using System.Web.UI.HtmlControls;