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);
我覺得這是我第一次看到「添加()」方法,這種方式工作。不應該在引擎蓋下創建給定參數(單元對象)的新實例嗎?如果有人能解釋這背後的邏輯是什麼,我將不勝感激。謝謝