MiscExtensions.cs 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using System.Web.UI;
  7. namespace MonoTests.Common
  8. {
  9. public static class MiscExtensions
  10. {
  11. public static TChild FindChild <TChild> (this Control parent) where TChild: class
  12. {
  13. return FindChild <TChild> (parent, null);
  14. }
  15. public static TChild FindChild<TChild> (this Control parent, string id) where TChild: class
  16. {
  17. if (parent == null)
  18. return null;
  19. foreach (Control child in parent.Controls) {
  20. if (child == null)
  21. continue;
  22. if (typeof (TChild).IsAssignableFrom (child.GetType ())) {
  23. if (String.IsNullOrEmpty (id))
  24. return child as TChild;
  25. if (String.Compare (child.ID, id, StringComparison.OrdinalIgnoreCase) == 0)
  26. return child as TChild;
  27. }
  28. TChild ret = child.FindChild<TChild> (id);
  29. if (ret != null)
  30. return ret;
  31. }
  32. return null;
  33. }
  34. }
  35. }