在动态HTML表中查找动态控件

时间:2012-09-11 19:28:00

标签: c# asp.net

我试图在动态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++;
}

现在,当我尝试从单元格中获取数据时,它会失败。我试过了:

this.FindControl("radApprove1");

但这会返回null。我也尝试迭代表中的行,但它只指示1行,这是标题。这段代码是从Page_Load调用的,所以我认为我可以获得这些控件。当我在呈现页面后查看HTML的HTML源代码时,我可以看到它们已被正确呈现。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

this.FindControl( “radApprove1”);只检查控件的主INamingContainer。 你需要递归地找到它或者做:

伪:

foreach row in a table.Rows
  foreach cell in row
    cell.FindControl()

答案 1 :(得分:0)

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

不完全确定以上是否有效。您为什么不考虑使用<asp:Table>来处理此问题?

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

希望这有帮助。