2014-01-06 40 views
0

我有一個webform,想要運行一個方法從窗體中返回所有控件ID的列表,以便將列表導出到Excel等其他位置(複製並粘貼另一個對象是好的)。獲取webform中所有控件的列表

下面有什麼,我想回到一個例子:

lblName 
lblAddress 
txtEmail 
ddlSelection 

我怎麼能去這樣做?

+0

請搜索SO,因爲此問題被問了很多次。 – OldProgrammer

+0

@connersz,試試這個鏈接:http://stackoverflow.com/questions/277646/finding-all-controls-in-an-asp-net-panel –

+0

我看過這個例子,但我不想做點什麼特別是在另一個控件內的某些控件。我實際上想要將網絡表單中的每個控件都列出來。 – connersz

回答

1
// Create a method to loop through 
// the control tree and record ids 
public void GetAllControlIDs(Control c, List<String> ids) 
{ 
    ids.Add(c.ID); 
    if(c.HasControls()) 
     foreach(Control ch in c.Controls) 
      GetAllControlIDs(ch, ids); 
} 

// Call your method and pass in the 
// Form since your question asks for 
// the Form controls 
List<String> allControlIDs = new List<String>(); 
GetAllControlIDs(this.Page.Form, allControlIDs); 

foreach (String id in allControlIDs) 
    Console.WriteLine(id);