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