ParameterCollection.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // System.Web.UI.WebControls/ParameterCollection.cs
  3. //
  4. // Author: Todd Berman <[email protected]>
  5. //
  6. // (C) 2003 Todd Berman
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. #if NET_2_0
  28. using System.Web.UI;
  29. using System.Collections;
  30. using System.Collections.Specialized;
  31. namespace System.Web.UI.WebControls
  32. {
  33. public class ParameterCollection : StateManagedCollection
  34. {
  35. private static Type[] _knownTypes = new Type[] {
  36. typeof (ControlParameter),
  37. typeof (CookieParameter),
  38. typeof (FormParameter),
  39. typeof (Parameter),
  40. typeof (QueryStringParameter),
  41. typeof (SessionParameter) };
  42. private EventHandler _parametersChanged;
  43. private KeyedList _values;
  44. public int Add (Parameter param)
  45. {
  46. return ((IList)this).Add (param);
  47. }
  48. public int Add (string name, string value)
  49. {
  50. return ((IList)this).Add (new Parameter (name, TypeCode.Object, value));
  51. }
  52. public int Add (string name, TypeCode type, string value)
  53. {
  54. return ((IList)this).Add (new Parameter (name, type, value));
  55. }
  56. protected override object CreateKnownType (int idx)
  57. {
  58. switch (idx) {
  59. case 0:
  60. return new ControlParameter ();
  61. break;
  62. case 1:
  63. return new CookieParameter ();
  64. break;
  65. case 2:
  66. return new FormParameter ();
  67. break;
  68. case 3:
  69. return new Parameter ();
  70. break;
  71. case 4:
  72. return new QueryStringParameter ();
  73. break;
  74. case 5:
  75. return new SessionParameter ();
  76. break;
  77. }
  78. throw new ArgumentOutOfRangeException ("index");
  79. }
  80. protected override Type[] GetKnownTypes ()
  81. {
  82. return _knownTypes;
  83. }
  84. public IOrderedDictionary GetValues (Control control)
  85. {
  86. if (_values == null)
  87. {
  88. _values = new KeyedList ();
  89. foreach (Parameter param in this)
  90. {
  91. string name = param.Name;
  92. for (int i = 1; _values.Contains (name); i++)
  93. {
  94. name = param.Name + i.ToString ();
  95. }
  96. _values.Add (name, param.ParameterValue);
  97. }
  98. }
  99. return _values;
  100. }
  101. public void Insert (int idx, Parameter param)
  102. {
  103. ((IList)this).Insert (idx, param);
  104. }
  105. protected override void OnClearComplete ()
  106. {
  107. base.OnClearComplete ();
  108. OnParametersChanged (EventArgs.Empty);
  109. }
  110. protected override void OnInsert (int idx, object value)
  111. {
  112. base.OnInsert (idx, value);
  113. ((Parameter)value).SetOwnerCollection (this);
  114. }
  115. protected override void OnInsertComplete (int idx, object value)
  116. {
  117. base.OnInsertComplete (idx, value);
  118. OnParametersChanged (EventArgs.Empty);
  119. }
  120. protected virtual void OnParametersChanged (EventArgs e)
  121. {
  122. if (_parametersChanged != null)
  123. _parametersChanged(this, e);
  124. _values = null;
  125. }
  126. protected override void OnValidate (object o)
  127. {
  128. base.OnValidate (o);
  129. if ((o is Parameter) == false)
  130. throw new ArgumentException ("o is not a Parameter");
  131. }
  132. public void Remove (Parameter param)
  133. {
  134. ((IList)this).Remove (param);
  135. }
  136. public void RemoveAt (int idx)
  137. {
  138. ((IList)this).RemoveAt (idx);
  139. }
  140. [MonoTODO("eh?")]
  141. protected override void SetDirtyObject (object o)
  142. {
  143. throw new NotImplementedException ();
  144. }
  145. internal void CallOnParameterChanged ()
  146. {
  147. OnParametersChanged (EventArgs.Empty);
  148. }
  149. private int IndexOfString (string name)
  150. {
  151. for (int i = 0; i < Count; i++)
  152. {
  153. if (((Parameter)((IList)this)[i]).Name == name)
  154. return i;
  155. }
  156. return -1;
  157. }
  158. public Parameter this[int idx] {
  159. get {
  160. return (Parameter) ((IList)this)[idx];
  161. }
  162. set {
  163. ((IList)this)[idx] = value;
  164. }
  165. }
  166. public Parameter this[string name] {
  167. get {
  168. int idx = IndexOfString (name);
  169. if (idx == -1)
  170. return null;
  171. return ((Parameter) ((IList)this)[idx]);
  172. }
  173. set {
  174. int idx = IndexOfString (name);
  175. if (idx == -1) {
  176. Add (value);
  177. return;
  178. }
  179. ((IList)this)[idx] = value;
  180. }
  181. }
  182. public event EventHandler ParametersChanged {
  183. add { _parametersChanged += value; }
  184. remove { _parametersChanged -= value; }
  185. }
  186. }
  187. }
  188. #endif