ControlCollection.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // System.Web.UI.ControlCollection.cs
  3. //
  4. // Duncan Mak ([email protected])
  5. //
  6. // (C) Ximian, Inc.
  7. //
  8. using System;
  9. using System.Collections;
  10. namespace System.Web.UI {
  11. public class ControlCollection : ICollection, IEnumerable
  12. {
  13. ArrayList list;
  14. Control owner;
  15. public ControlCollection (Control owner)
  16. {
  17. if (owner == null)
  18. throw new ArgumentException ();
  19. list = new ArrayList ();
  20. this.owner = owner;
  21. }
  22. internal ControlCollection (Control owner, bool shortList)
  23. {
  24. if (owner == null)
  25. throw new ArgumentException ();
  26. list = new ArrayList (shortList ? 1 : 0);
  27. this.owner = owner;
  28. }
  29. public int Count {
  30. get { return list.Count; }
  31. }
  32. public bool IsReadOnly {
  33. get { return list.IsReadOnly; }
  34. }
  35. public bool IsSynchronized {
  36. get { return list.IsSynchronized; }
  37. }
  38. public virtual Control this [int index] {
  39. get { return list [index] as Control; }
  40. }
  41. protected Control Owner {
  42. get { return owner; }
  43. }
  44. public object SyncRoot {
  45. get { return list.SyncRoot; }
  46. }
  47. public virtual void Add (Control child)
  48. {
  49. if (child == null)
  50. throw new ArgumentNullException ();
  51. if (IsReadOnly)
  52. throw new HttpException ();
  53. list.Add (child);
  54. owner.AddedControl (child, list.Count - 1);
  55. }
  56. public virtual void AddAt (int index, Control child)
  57. {
  58. if (child == null) // maybe we should check for ! (child is Control)?
  59. throw new ArgumentNullException ();
  60. if ((index < -1) || (index > Count))
  61. throw new ArgumentOutOfRangeException ();
  62. if (IsReadOnly)
  63. throw new HttpException ();
  64. if (index == -1){
  65. Add (child);
  66. } else {
  67. list.Insert (index, child);
  68. owner.AddedControl (child, index);
  69. }
  70. }
  71. public virtual void Clear ()
  72. {
  73. list.Clear ();
  74. if (owner != null)
  75. owner.ResetChildNames ();
  76. }
  77. public virtual bool Contains (Control c)
  78. {
  79. return list.Contains (c);
  80. }
  81. public void CopyTo (Array array, int index)
  82. {
  83. list.CopyTo (array, index);
  84. }
  85. public IEnumerator GetEnumerator ()
  86. {
  87. return list.GetEnumerator ();
  88. }
  89. public virtual int IndexOf (Control c)
  90. {
  91. return list.IndexOf (c);
  92. }
  93. public virtual void Remove (Control value)
  94. {
  95. list.Remove (value);
  96. owner.RemovedControl (value);
  97. }
  98. public virtual void RemoveAt (int index)
  99. {
  100. if (IsReadOnly)
  101. throw new HttpException ();
  102. Control value = (Control) list [index];
  103. list.RemoveAt (index);
  104. owner.RemovedControl (value);
  105. }
  106. }
  107. }