using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace GreenTree.Nachtragsmanagement.Web.Extensions
{
    public static class ControlHelper
    {
        public static Control[] GetAllSubControlsOfTypes(this Control control, params Type[] types)
        {
            var result = new List<Control>();

            GetSubControls(control, result, types);

            return result.ToArray();
        }

        private static void GetSubControls(Control parent, List<Control> result, Type[] types)
        {
            if (parent == null) return;

            foreach (Control subControl in parent.Controls)
            {
                if (subControl.Controls.Count > 0)
                    GetSubControls(subControl, result, types);

                foreach (var type in types)
                {
                    if (subControl.GetType() == type)
                    {
                        result.Add(subControl);
                        break;
                    }
                }
            }
        }
    }
}