2010-01-29 84 views
1

我有一個HTML表「表」,並在運行時添加的控件它:獲取的動態標識加控制

HtmlTableRow tabelrow = new HtmlTableRow(); 

HtmlTableCell tablecell = new HtmlTableCell(); 

Label lbl = new Label(); 

tablecell.Controls.Add(lbl); 

然後我想用的foreach

(foreach control c in table.controls)讓每個控件或(foreach control c in tablecell.controls) 但它不工作。

+1

你可以發佈你使用的代碼,以便我們可以更好地幫助你嗎? – jessegavin 2010-01-29 05:45:51

回答

1

請嘗試以下操作。我可以使用ID獲取控件,但您需要首先爲其分配一個ID。

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); 
    } 

你究竟想要做什麼?請詳細說明。