-1

我正在尝试在动态 HTML 表中找到动态控件。我有以下内容:

System.Web.UI.HtmlControls.HtmlTableRow TR = null;
System.Web.UI.HtmlControls.HtmlTableCell TD1 = null;
System.Web.UI.HtmlControls.HtmlTableCell TD2 = null;
System.Web.UI.HtmlControls.HtmlTableCell TD3 = null;
System.Web.UI.HtmlControls.HtmlTableCell TD4 = null;
System.Web.UI.HtmlControls.HtmlTableCell TD5 = null;
System.Web.UI.HtmlControls.HtmlTableCell TD6 = null;

// Database stuff
int i = 1;
while (objRdr.Read())
{
    TR = new System.Web.UI.HtmlControls.HtmlTableRow();
    TD1 = new System.Web.UI.HtmlControls.HtmlTableCell();
    TD2 = new System.Web.UI.HtmlControls.HtmlTableCell();
    TD3 = new System.Web.UI.HtmlControls.HtmlTableCell();
    TD4 = new System.Web.UI.HtmlControls.HtmlTableCell();
    TD5 = new System.Web.UI.HtmlControls.HtmlTableCell();
    TD6 = new System.Web.UI.HtmlControls.HtmlTableCell();

    // ASP code maybe wrong, but its to give you the ID.
    TD1.InnerHtml = "<asp:RadioButton runat='server' ID='radApprove"+i.ToString()+"' Value='Approve' />";
    TD2.InnerText = HttpUtility.HtmlEncode(objRdr[1].ToString());
    TD3.InnerText = HttpUtility.HtmlEncode(objRdr[6].ToString());
    TD4.InnerText = HttpUtility.HtmlEncode(objRdr[2].ToString());
    TD5.InnerText = HttpUtility.HtmlEncode(objRdr[10].ToString());
    TD6.InnerText = HttpUtility.HtmlEncode(objRdr[8].ToString());

    TR.Cells.Add(TD1);
    TR.Cells.Add(TD2);
    TR.Cells.Add(TD3);
    TR.Cells.Add(TD4);
    TR.Cells.Add(TD5);
    TR.Cells.Add(TD6);

    tblOrderItems.Rows.Add(TR);
    TR = null;
    TD1 = null;
    TD2 = null;
    TD3 = null;
    TD4 = null;
    TD5 = null;
    TD6 = null;
    i++;
}

现在,当我尝试从 Cells 中获取数据时,它失败了。我努力了:

this.FindControl("radApprove1");

但这会返回一个null. 我也尝试迭代表中的行,但它只表示 1 行,即 Header。这段代码是从 中调用的Page_Load,所以我想我可以得到这些控件。当我在呈现页面后查看其 HTML 源代码时,我可以看到它们已正确呈现。有任何想法吗?

4

2 回答 2

0
TD1.InnerHtml = "<asp:RadioButton runat='server' ID='radApprove"+i.ToString()+"' Value='Approve' />"

不完全确定上述是否可行。为什么不考虑使用<asp:Table>for 来处理这个问题?

有一种在有限程度上查找子控件的技术:http ://weblogs.asp.net/dwahlin/archive/2006/08/25/Finding-ASP.NET-Child-Controls_2E002E002E002E00_The-Simple-Way.aspx

希望这可以帮助。

于 2012-09-12T01:47:54.570 回答
0

this.FindControl("radApprove1"); 仅检查您的控件的主 INamingContainer。您需要递归地找到它或执行以下操作:

伪:

foreach row in a table.Rows
  foreach cell in row
    cell.FindControl()
于 2012-09-11T20:40:50.830 回答