Thursday, November 13, 2008

Finding All Controls on Page in Asp.net

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

3 comments:

Anonymous said...

Thnkx . this solved my problem .

Anonymous said...

Perfect...exactly what I was looking for. Thanks!

Girish Dindukurthy said...

How to get only DIV controls from the page??

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape