CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 25, 2006

Get All Controls in ASP.NET

 protected Control[] GetAllControls() 
 {
   ArrayList allControls = new ArrayList();
   Queue queue = new Queue();
   queue.Enqueue(this.Controls);

   while( queue.Count > 0 ) 
   {
      Control.ControlCollection
       controls = (Control.ControlCollection)queue.Dequeue();
      if( controls == null || controls.Count == 0 ) 
         continue;
      foreach( Control control in controls ) 
      {
           allControls.Add(control);
         queue.Enqueue(control.Controls);
      }
   }

   return (Control[])allControls.ToArray(typeof(Control));
 }

Post a Comment