DataPagerFieldCollection.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Web.UI.WebControls.DataPagerFieldCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007-2008 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.UI.WebControls
  36. {
  37. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class DataPagerFieldCollection : StateManagedCollection
  40. {
  41. enum KnownTypeIndexes
  42. {
  43. NextPreviousPagerField = 0,
  44. NumericPagerField = 1,
  45. TemplatePagerField = 2
  46. }
  47. static readonly Type[] knownTypes = {
  48. typeof (NextPreviousPagerField),
  49. typeof (NumericPagerField),
  50. typeof (TemplatePagerField)
  51. };
  52. static readonly object FieldsChangedEvent = new object ();
  53. IList list;
  54. DataPager owner;
  55. EventHandlerList events;
  56. public event EventHandler FieldsChanged {
  57. add { AddEventHandler (FieldsChangedEvent, value); }
  58. remove { RemoveEventHandler (FieldsChangedEvent, value); }
  59. }
  60. public DataPagerFieldCollection (DataPager dataPager)
  61. {
  62. list = (IList)this;
  63. owner = dataPager;
  64. }
  65. public void Add (DataPagerField field)
  66. {
  67. list.Add (field);
  68. }
  69. public DataPagerFieldCollection CloneFields (DataPager pager)
  70. {
  71. throw new NotImplementedException ();
  72. }
  73. public bool Contains (DataPagerField field)
  74. {
  75. return list.Contains (field);
  76. }
  77. public void CopyTo (DataPagerField[] array, int index)
  78. {
  79. }
  80. protected override object CreateKnownType (int index)
  81. {
  82. if (!Enum.IsDefined (typeof (KnownTypeIndexes), index))
  83. throw new ArgumentOutOfRangeException ("index");
  84. return Activator.CreateInstance (knownTypes [index]);
  85. }
  86. protected override Type[] GetKnownTypes ()
  87. {
  88. return knownTypes;
  89. }
  90. public int IndexOf (DataPagerField field)
  91. {
  92. return list.IndexOf (field);
  93. }
  94. public void Insert (int index, DataPagerField field)
  95. {
  96. list.Insert (index, field);
  97. }
  98. protected override void OnClearComplete ()
  99. {
  100. base.OnClearComplete ();
  101. InvokeEvent (FieldsChangedEvent, EventArgs.Empty);
  102. }
  103. protected override void OnInsertComplete (int index, object value)
  104. {
  105. base.OnInsertComplete (index, value);
  106. DataPagerField field = value as DataPagerField;
  107. if (field == null)
  108. return;
  109. field.SetDataPager (owner);
  110. field.FieldChanged += new EventHandler (FieldHasChanged);
  111. InvokeEvent (FieldsChangedEvent, EventArgs.Empty);
  112. }
  113. protected override void OnRemoveComplete (int index, object value)
  114. {
  115. base.OnRemoveComplete (index, value);
  116. DataPagerField field = value as DataPagerField;
  117. if (field == null)
  118. return;
  119. field.SetDataPager (null);
  120. field.FieldChanged -= new EventHandler (FieldHasChanged);
  121. InvokeEvent (FieldsChangedEvent, EventArgs.Empty);
  122. }
  123. protected override void OnValidate (object o)
  124. {
  125. base.OnValidate (o);
  126. DataPagerField field = o as DataPagerField;
  127. if (field == null)
  128. throw new ArgumentException ("is not an instance of the DataPagerField class or of one of its derived classes.", "o");
  129. }
  130. public void Remove (DataPagerField field)
  131. {
  132. list.Remove (field);
  133. }
  134. public void RemoveAt (int index)
  135. {
  136. list.RemoveAt (index);
  137. }
  138. protected override void SetDirtyObject (object o)
  139. {
  140. }
  141. [BrowsableAttribute(false)]
  142. public DataPagerField this [int index] {
  143. get { return list [index] as DataPagerField; }
  144. }
  145. void FieldHasChanged (object sender, EventArgs args)
  146. {
  147. InvokeEvent (FieldsChangedEvent, EventArgs.Empty);
  148. }
  149. void AddEventHandler (object key, EventHandler handler)
  150. {
  151. if (events == null)
  152. events = new EventHandlerList ();
  153. events.AddHandler (key, handler);
  154. }
  155. void RemoveEventHandler (object key, EventHandler handler)
  156. {
  157. if (events == null)
  158. return;
  159. events.RemoveHandler (key, handler);
  160. }
  161. void InvokeEvent (object key, EventArgs args)
  162. {
  163. if (events == null)
  164. return;
  165. EventHandler eh = events [key] as EventHandler;
  166. if (eh == null)
  167. return;
  168. eh (this, args);
  169. }
  170. }
  171. }