2014-12-02 63 views
0
ContentPlaceHolder wrapperCPH = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1"); 
    HtmlTable tblCumulativeCredits = (HtmlTable)wrapperCPH.FindControl("tblCumulativeCredits"); 
    //If your table is not inside of a ContentPlaceHolder, you can just test with "this.FindControl("yourHTMLTableID"); 

    HtmlTableRow tableRow = new HtmlTableRow(); 
    HtmlTableCell tableCellAmount = new HtmlTableCell(); 
    HtmlTableCell tableCellCurrency = new HtmlTableCell(); 

    tableCellAmount.InnerText = item.amount.ToString(); 
    tableCellCurrency.InnerText = item.currencyCode.code; 

    tableRow.Cells.Add(tableCellAmount); 
    tableRow.Cells.Add(tableCellCurrency); 
    tableRow.Cells.Add(tableCellCurrency); 

    tblCumulativeCredits.Rows.Add(tableRow); 

HTML:HtmlTableCellCollection.Add()每次都需要NEW Cell实例?

<table id="tblCumulativeCredits" runat="server"> 
    <tr> 
     <th class="th">Amount</th> 
     <th class="th">Currency1</th> 
     <th class="th">Currency2</th>   
    </tr> 
</table> 

现在想象一下,我加入在循环中使用此代码示例行,我知道我的CURRENCY1 = Currency2在某些情况下。所以我使用与上例相同的Cell对象。但是,如果我添加像例子一样的货币,有一些有趣的事情,它只添加第一个,第三个单元(Currency3)是空的。 如果我创建一个细胞的实例,并添加它,它只是增加了第二个货币:

tableRow.Cells.Add(tableCellAmount); 
    tableRow.Cells.Add(tableCellCurrency1); 
    tableRow.Cells.Add(tableCellCurrency2); 

我觉得这是我第一次看到“添加()”方法,这种方式工作。不应该在引擎盖下创建给定参数(单元对象)的新实例吗?如果有人能解释这背后的逻辑是什么,我将不胜感激。谢谢

回答

0

我认为它与“添加”方法的实现无关,而是我认为这发生在渲染过程中。 只要记住,你添加了两次相同的实例,这不仅意味着单元格的值(也可能是某种引用“覆盖”行为的参考)。 所以,你最好创建一个新的实例。

干杯

相关问题