Get Id of dynamically added control

2019-07-29 07:06发布

i have one html table "table "and added controls it at run time as :

HtmlTableRow tabelrow = new HtmlTableRow();

HtmlTableCell tablecell = new HtmlTableCell();

Label lbl = new Label();

tablecell.Controls.Add(lbl);

then i want to get each controls by using foreach

such as (foreach control c in table.controls) or (foreach control c in tablecell.controls) but its not working.

标签: asp.net
1条回答
虎瘦雄心在
2楼-- · 2019-07-29 07:53

Tried the following. I can get the controls with the ID but you need to assign an ID to it first.

HtmlTable table = new HtmlTable();

   for (int i = 0; i < 10; i++)
   {
      HtmlTableRow tablerow = new HtmlTableRow();
      tablerow.ID = Guid.NewGuid().ToString();

      HtmlTableCell tablecell = new HtmlTableCell();
      tablecell.ID = Guid.NewGuid().ToString();

      Label lbl = new Label();
      lbl.ID = Guid.NewGuid().ToString();

      tablecell.Controls.Add(lbl);
      tablerow.Controls.Add(tablecell);
      table.Controls.Add(tablerow);
   }

   List<string>RowID = new List<string>();
   List<string>CellID = new List<string>();
   List<string>LabelID = new List<string>();

   foreach (Control Row in table.Controls)
   {
      //Each control will be a tablerow.
      RowID.Add(Row.ID);

      CellID.Add(Row.Controls[0].ID);

      LabelID.Add(Row.Controls[0].Controls[0].ID);
   }

What exactly are you trying to do? Please elaborate.

查看更多
登录 后发表回答