2014-12-02 61 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

我認爲它與「添加」方法的實現無關,而是我認爲這發生在渲染過程中。 只要記住,你添加了兩次相同的實例,這不僅意味着單元格的值(也可能是某種引用「覆蓋」行爲的參考)。 所以,你最好創建一個新的實例。

乾杯

相關問題