ParameterCollection.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. using System.ComponentModel;
  32. namespace System.Web.UI.WebControls
  33. {
  34. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  35. public class ParameterCollection : StateManagedCollection
  36. {
  37. private static Type[] _knownTypes = new Type[] {
  38. typeof (ControlParameter),
  39. typeof (CookieParameter),
  40. typeof (FormParameter),
  41. typeof (Parameter),
  42. typeof (QueryStringParameter),
  43. typeof (SessionParameter) };
  44. private EventHandler _parametersChanged;
  45. public int Add (Parameter param)
  46. {
  47. return ((IList)this).Add (param);
  48. }
  49. public int Add (string name, string value)
  50. {
  51. return ((IList)this).Add (new Parameter (name, TypeCode.Object, value));
  52. }
  53. public int Add (string name, TypeCode type, string value)
  54. {
  55. return ((IList)this).Add (new Parameter (name, type, value));
  56. }
  57. protected override object CreateKnownType (int idx)
  58. {
  59. switch (idx) {
  60. case 0:
  61. return new ControlParameter ();
  62. case 1:
  63. return new CookieParameter ();
  64. case 2:
  65. return new FormParameter ();
  66. case 3:
  67. return new Parameter ();
  68. case 4:
  69. return new QueryStringParameter ();
  70. case 5:
  71. return new SessionParameter ();
  72. }
  73. throw new ArgumentOutOfRangeException ("index");
  74. }
  75. protected override Type[] GetKnownTypes ()
  76. {
  77. return _knownTypes;
  78. }
  79. public IOrderedDictionary GetValues (HttpContext context, Control control)
  80. {
  81. OrderedDictionary values = new OrderedDictionary ();
  82. foreach (Parameter param in this)
  83. {
  84. string name = param.Name;
  85. for (int i = 1; values.Contains (name); i++)
  86. name = param.Name + i.ToString ();
  87. values.Add (name, param.GetValue (context, control));
  88. }
  89. return values;
  90. }
  91. public void UpdateValues (HttpContext context, Control control)
  92. {
  93. foreach (Parameter param in this)
  94. param.GetValue (context, control);
  95. }
  96. public void Insert (int idx, Parameter param)
  97. {
  98. ((IList)this).Insert (idx, param);
  99. }
  100. protected override void OnClearComplete ()
  101. {
  102. base.OnClearComplete ();
  103. OnParametersChanged (EventArgs.Empty);
  104. }
  105. protected override void OnInsert (int idx, object value)
  106. {
  107. base.OnInsert (idx, value);
  108. ((Parameter)value).SetOwnerCollection (this);
  109. }
  110. protected override void OnInsertComplete (int idx, object value)
  111. {
  112. base.OnInsertComplete (idx, value);
  113. OnParametersChanged (EventArgs.Empty);
  114. }
  115. protected virtual void OnParametersChanged (EventArgs e)
  116. {
  117. if (_parametersChanged != null)
  118. _parametersChanged(this, e);
  119. }
  120. protected override void OnValidate (object o)
  121. {
  122. base.OnValidate (o);
  123. if ((o is Parameter) == false)
  124. throw new ArgumentException ("o is not a Parameter");
  125. }
  126. public void Remove (Parameter param)
  127. {
  128. ((IList)this).Remove (param);
  129. }
  130. public void RemoveAt (int idx)
  131. {
  132. ((IList)this).RemoveAt (idx);
  133. }
  134. protected override void SetDirtyObject (object o)
  135. {
  136. Parameter param = (Parameter)o;
  137. if (Contains (param))
  138. param.SetDirty ();
  139. }
  140. internal void CallOnParameterChanged ()
  141. {
  142. OnParametersChanged (EventArgs.Empty);
  143. }
  144. private int IndexOfString (string name)
  145. {
  146. for (int i = 0; i < Count; i++)
  147. {
  148. if (((Parameter)((IList)this)[i]).Name == name)
  149. return i;
  150. }
  151. return -1;
  152. }
  153. public Parameter this[int idx] {
  154. get {
  155. return (Parameter) ((IList)this)[idx];
  156. }
  157. set {
  158. ((IList)this)[idx] = value;
  159. }
  160. }
  161. public Parameter this[string name] {
  162. get {
  163. int idx = IndexOfString (name);
  164. if (idx == -1)
  165. return null;
  166. return ((Parameter) ((IList)this)[idx]);
  167. }
  168. set {
  169. int idx = IndexOfString (name);
  170. if (idx == -1) {
  171. Add (value);
  172. return;
  173. }
  174. ((IList)this)[idx] = value;
  175. }
  176. }
  177. public event EventHandler ParametersChanged {
  178. add { _parametersChanged += value; }
  179. remove { _parametersChanged -= value; }
  180. }
  181. public bool Contains (Parameter param)
  182. {
  183. return ((IList)this).Contains (param);
  184. }
  185. public void CopyTo (Parameter[] paramArray, int index)
  186. {
  187. ((IList)this).CopyTo (paramArray, index);
  188. }
  189. public int IndexOf (Parameter param)
  190. {
  191. return ((IList)this).IndexOf (param);
  192. }
  193. protected override void OnRemoveComplete (int index, object value)
  194. {
  195. base.OnRemoveComplete (index, value);
  196. OnParametersChanged (EventArgs.Empty);
  197. }
  198. }
  199. }
  200. #endif