Finding All Controls on Page in Asp.net
To find all controls on Page, including child controls. i.e. Loop through all control list.
Example: I have a page with Controls on Page, Pannel Controls (Which contains Few more child controls). Now I want to find list of all controls on Page including child control.
private void ListControlCollections()
{
ArrayList controlList = new ArrayList();
AddControls(Page.Controls,controlList);
foreach (string str in controlList)
{
Response.Write(str + "<br/>");
}
Response.Write("Total Controls:" + controlList.Count);
}
private void AddControls(ControlCollection page,ArrayList controlList)
{
foreach (Control c in page)
{
if (c.ID != null)
{
controlList.Add(c.ID);
}
if(c.HasControls())
{
AddControls(c.Controls, controlList);
}
}
}
//OUTPUT of Code
form1
Panel1
Label1
TextBox1
Label2
TextBox2
Label3
TextBox3
Label10
TextBox10
Label11
TextBox11
Label12
TextBox12
btnControls
lblResult
Total Controls:16