ControlCollection.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // System.Web.UI.ControlCollection.cs
  3. //
  4. // Duncan Mak ([email protected])
  5. //
  6. // (C) Ximian, Inc.
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. namespace System.Web.UI {
  31. public class ControlCollection : ICollection, IEnumerable
  32. {
  33. ArrayList list;
  34. Control owner;
  35. public ControlCollection (Control owner)
  36. {
  37. if (owner == null)
  38. throw new ArgumentException ();
  39. list = new ArrayList ();
  40. this.owner = owner;
  41. }
  42. internal ControlCollection (Control owner, bool shortList)
  43. {
  44. if (owner == null)
  45. throw new ArgumentException ();
  46. list = new ArrayList (shortList ? 1 : 0);
  47. this.owner = owner;
  48. }
  49. public int Count {
  50. get { return list.Count; }
  51. }
  52. public bool IsReadOnly {
  53. get { return list.IsReadOnly; }
  54. }
  55. public bool IsSynchronized {
  56. get { return list.IsSynchronized; }
  57. }
  58. public virtual Control this [int index] {
  59. get { return list [index] as Control; }
  60. }
  61. protected Control Owner {
  62. get { return owner; }
  63. }
  64. public object SyncRoot {
  65. get { return list.SyncRoot; }
  66. }
  67. public virtual void Add (Control child)
  68. {
  69. if (child == null)
  70. throw new ArgumentNullException ();
  71. if (IsReadOnly)
  72. throw new HttpException ();
  73. list.Add (child);
  74. owner.AddedControl (child, list.Count - 1);
  75. }
  76. public virtual void AddAt (int index, Control child)
  77. {
  78. if (child == null) // maybe we should check for ! (child is Control)?
  79. throw new ArgumentNullException ();
  80. if ((index < -1) || (index > Count))
  81. throw new ArgumentOutOfRangeException ();
  82. if (IsReadOnly)
  83. throw new HttpException ();
  84. if (index == -1){
  85. Add (child);
  86. } else {
  87. list.Insert (index, child);
  88. owner.AddedControl (child, index);
  89. }
  90. }
  91. public virtual void Clear ()
  92. {
  93. foreach (Control ctrl in list)
  94. owner.RemovedControl (ctrl);
  95. list.Clear ();
  96. if (owner != null)
  97. owner.ResetChildNames ();
  98. }
  99. public virtual bool Contains (Control c)
  100. {
  101. return list.Contains (c);
  102. }
  103. public void CopyTo (Array array, int index)
  104. {
  105. list.CopyTo (array, index);
  106. }
  107. public IEnumerator GetEnumerator ()
  108. {
  109. return list.GetEnumerator ();
  110. }
  111. public virtual int IndexOf (Control c)
  112. {
  113. return list.IndexOf (c);
  114. }
  115. public virtual void Remove (Control value)
  116. {
  117. list.Remove (value);
  118. owner.RemovedControl (value);
  119. }
  120. public virtual void RemoveAt (int index)
  121. {
  122. if (IsReadOnly)
  123. throw new HttpException ();
  124. Control value = (Control) list [index];
  125. list.RemoveAt (index);
  126. owner.RemovedControl (value);
  127. }
  128. }
  129. }