我试图从动态ASP.Net表中读取元素。
该表在ASP页面中声明如下:
<table id="ItemTable" runat="server">
<!-- Will be filled with the user items. -->
</table>
我添加了使用此类声明的项目:
public void AddDiv(shopItem item, int count)
{
ContentPlaceHolder myCPH = (ContentPlaceHolder)this.Master.FindControl("PageContent"); // since i'm in a child of a master page, First i'm finding the CPH
HtmlTable Table = (HtmlTable)myCPH.FindControl("ItemTable");//In the page, I'm finding the table
Control possibleInstance = Table.FindControl("AmountFor" + item.ID); // here i'm looking for an element in the table which should be declared if I already created that item.
if (possibleInstance != null) // making sure there are no duplicates.
{
return;
}
//more meaningless code
TextBox Amount = new TextBox();
Amount.ID = "AmoutFor" + item.ID;
// some more less relevant code
myCell.Controls.Add(Amount);
// even more meaningless code
rows[rows.Count - 1].Controls.Add(myCell);
foreach (var tr in rows)
{
ItemTable.Controls.Add(tr);
}
}
它工作正常 - 问题是,当我试图找到重复项时,它找到了表 - 但它找不到动态创建的单元格。我在if
子句的末尾设置了一个断点,由于某种原因,虽然已经声明了名为AmountFor2
的元素,并且找到了表 - 但它找不到具有该ID的元素,它创建了另一个重复的div。我很确定这是因为元素是动态创建的;所以,我的问题是,如何找到动态陈述的元素?