ASP.NET - Get controls on the web page - Asked By mostafa hamdy on 30-May-11 11:54 AM

Hello all
I have some web page contains many controls and Textbox, lables and some others controls
I do the following code:

foreach (Control cntrl in Page.Controls)

{

if (cntrl is TextBox)

{

}
but I didnt get any controls with in the page , and I couldnt access them, please if any body can tell me how can I get the controls within the web page in a tested way , please send me or tell me about some URL may help me in doing that
regards
Mostafa

dipa ahuja replied to mostafa hamdy on 30-May-11 12:28 PM
Try this way..
foreach (Control c in Page.Controls)
    {
      foreach (Control ctrl in c.Controls)
      {
        if (ctrl is TextBox)
        {
          string value=((TextBox)ctrl).Text.ToString();
        }
      }
    }
Nikhil Mahajan replied to mostafa hamdy on 30-May-11 12:57 PM
Finding All Controls on Page in Asp.net

To find all controls on Page, including child controls. i.e. Loop through all control list. use below code

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

Total Controls:5

thats what you want...


John Andrews replied to mostafa hamdy on 30-May-11 10:32 PM
This is great! I've gotta try myself too. Hope it will be effective. Thanks!
http://www.allconnect.com/
Jitendra Faye replied to mostafa hamdy on 30-May-11 11:23 PM
If you have place all control directly in web page then you can get those control by using this code-

foreach( Control c in this.Controls)
 {
   if(c is TextBox)
      {
      //your code
     }
  }

But if you have placed your control in any container control then you have to give that container control name,
like this-

foreach( Control c in Panel1.Controls)  //here Panel1 is a container
 {
   if(c is TextBox)
      {
      //your code
     }
  }


try and let me know.

Anoop S replied to mostafa hamdy on 31-May-11 01:07 AM

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.

http://4.bp.blogspot.com/_nuQwSyDoLk8/SRxcY0WW4NI/AAAAAAAAAzc/wo4qxL1lLt4/s1600-h/Controls+Collection.bmp


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

http://dotnetguts.blogspot.com/2008/11/finding-all-controls-on-page-in-aspnet.html