无法动态添加单元格

时间:2014-03-17 09:09:47

标签: c# asp.net

这是我的一些aspx代码:

<table class="agentsStyle" id="agentsTable" runat="server">
                 <tr id="agentsNames">
                     <td>
                         ahmad
                     </td>
                 </tr>
</table>

这是我的c#代码:

TableCell cell = new TableCell();
            cell.Text = agentName;
            cell.Attributes.Add("class", "d");
            agentsNames.Cells.Add(cell);

我收到了这个错误:

Error   12  The best overloaded method match for 'System.Web.UI.HtmlControls.HtmlTableCellCollection.Add(System.Web.UI.HtmlControls.HtmlTableCell)' has some invalid arguments

3 个答案:

答案 0 :(得分:1)

您需要添加HtmlTableCell而不是TableCell。见MSDN。您还需要将Text属性更改为InnerText

所以试试这个:

HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = agentName;
cell.Attributes.Add("class", "d");
agentsNames.Cells.Add(cell);

答案 1 :(得分:1)

using System.Web.UI.HtmlControls;

...

 HtmlTableCell cell = new HtmlTableCell();
 cell.InnerText= "test";  //  change cell.Text to cell.InnerText
 cell.Attributes.Add("class", "d");
 agentsNames.Cells.Add(cell);

答案 2 :(得分:1)

替换它:

TableCell cell = new TableCell();

用这个

HtmlTableCell cell = new HtmlTableCell();