2009-06-29 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Parameter.cs
1 //
2 // System.Web.UI.WebControls.Parameter
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Sanjay Gupta (gsanjay@novell.com)
7 //
8 // (C) 2003 Ben Maurer
9 // (C) 2004 Novell, Inc. (http://www.novell.com)
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Text;
37 using System.Data;
38 using System.ComponentModel;
39
40 namespace System.Web.UI.WebControls {
41         [DefaultPropertyAttribute ("DefaultValue")]
42         public class Parameter : ICloneable, IStateManager 
43         {
44                 public Parameter () : base ()
45                 {
46                 }
47
48                 protected Parameter (Parameter original)
49                 {
50                         this.DefaultValue = original.DefaultValue;
51                         this.Direction = original.Direction;
52                         this.ConvertEmptyStringToNull = original.ConvertEmptyStringToNull;
53                         this.Type = original.Type;
54                         this.Name = original.Name;
55                 }
56                 
57                 public Parameter (string name)
58                 {
59                         this.Name = name;
60                 }
61                 
62                 public Parameter(string name, TypeCode type) : this (name)
63                 {
64                         this.Type = type;
65                 }
66                 
67                 public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
68                 {
69                         this.DefaultValue = defaultValue;
70                 }
71                 
72                 protected virtual Parameter Clone ()
73                 {
74                         return new Parameter (this);
75                 }
76                 
77                 protected void OnParameterChanged ()
78                 {
79                         if (_owner != null)
80                                 _owner.CallOnParameterChanged ();
81                 }
82                 
83                 protected virtual void LoadViewState (object savedState)
84                 {
85                         ViewState.LoadViewState (savedState);
86                 }
87                 
88                 protected virtual object SaveViewState ()
89                 {
90                         return ViewState.SaveViewState ();
91                 }
92                 
93                 protected virtual void TrackViewState ()
94                 {
95                         isTrackingViewState = true;
96                         if (viewState != null)
97                                 viewState.TrackViewState ();
98                 }
99                 
100                 object ICloneable.Clone ()
101                 {
102                         return this.Clone ();
103                 }
104                 
105                 void IStateManager.LoadViewState (object savedState)
106                 {
107                         this.LoadViewState (savedState);
108                 }
109                 
110                 object IStateManager.SaveViewState ()
111                 {
112                         return this.SaveViewState ();
113                 }
114                 
115                 void IStateManager.TrackViewState ()
116                 {
117                         this.TrackViewState ();
118                 }
119                 
120                 bool IStateManager.IsTrackingViewState {
121                         get { return this.IsTrackingViewState; }
122                 }
123                 
124                 // MSDN: The ToString method returns the Name property of the Parameter object. If the Parameter object has no name, ToString returns String.Empty.
125                 public override string ToString ()
126                 {
127                         return Name;
128                 }
129                 
130                 [WebCategoryAttribute ("Parameter")]
131                 [DefaultValueAttribute (null)]
132                 [WebSysDescriptionAttribute ("Default value to be used in case value is null.")]
133                 public string DefaultValue {
134                         get { return ViewState.GetString ("DefaultValue", null); }
135                         set {
136                                 
137                                 if (DefaultValue != value) {
138                                         ViewState ["DefaultValue"] = value;
139                                         OnParameterChanged ();
140                                 }
141                         }
142                 }
143
144                 [WebCategoryAttribute ("Parameter")]
145                 [DefaultValueAttribute ("Input")]
146                 [WebSysDescriptionAttribute ("Parameter's direction.")]
147                 public ParameterDirection Direction
148                 {
149                         get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
150                         set {                           
151                                 if (Direction != value) {
152                                         ViewState ["Direction"] = value;
153                                         OnParameterChanged ();
154                                 }
155                         }
156                 }
157
158
159                 [WebCategoryAttribute ("Parameter")]
160                 [DefaultValueAttribute ("")]
161                 [WebSysDescriptionAttribute ("Parameter's name.")]
162                 public string Name
163                 {
164                         get {
165                                 string s = ViewState ["Name"] as string;
166                                 if (s != null)
167                                         return s;
168                                 
169                                 return "";
170                         }
171                         set {
172                                 
173                                 if (Name != value) {
174                                         ViewState ["Name"] = value;
175                                         OnParameterChanged ();
176                                 }
177                         }
178                 }
179
180                 [WebCategoryAttribute ("Parameter")]
181                 [DefaultValueAttribute (true)]
182                 [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
183                 public bool ConvertEmptyStringToNull
184                 {
185                         get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
186                         set {
187                                 if (ConvertEmptyStringToNull != value) {
188                                         ViewState["ConvertEmptyStringToNull"] = value;
189                                         OnParameterChanged ();
190                                 }
191                         }
192                 }
193
194                 [WebCategoryAttribute ("Parameter")]
195                 [DefaultValueAttribute (DbType.Object)]
196                 [WebSysDescriptionAttribute ("Parameter's DbType.")]
197                 public DbType DbType
198                 {
199                         get {
200                                 object o = ViewState ["DbType"];
201                                 if (o == null)
202                                         return DbType.Object;
203                                 return (DbType) o;
204                         }
205                         set {
206                                 if (DbType != value) {
207                                         ViewState ["DbType"] = value;
208                                         OnParameterChanged ();
209                                 }
210                         }
211                 }
212
213                 [DefaultValue (0)]
214                 public int Size {
215                         get { return ViewState.GetInt ("Size", 0); }
216                         set {
217                                 if (Size != value) {
218                                         ViewState["Size"] = value;
219                                         OnParameterChanged ();
220                                 }
221                         }
222                 }
223
224                 [DefaultValueAttribute (TypeCode.Empty)]
225                 [WebCategoryAttribute ("Parameter"), 
226                 WebSysDescriptionAttribute("Represents type of the parameter.")]
227                 public TypeCode Type
228                 {
229                         get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
230                         set {
231                                 
232                                 if (Type != value) {
233                                         ViewState ["Type"] = value;
234                                         OnParameterChanged ();
235                                 }
236                         }
237                 }
238                 
239                 StateBag viewState;
240                 [BrowsableAttribute (false), 
241                 DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
242                 protected StateBag ViewState {
243                         get {
244                                 if (viewState == null) {
245                                         viewState = new StateBag ();
246                                         if (IsTrackingViewState)
247                                                 viewState.TrackViewState ();
248                                 }
249                                 return viewState;
250                         }
251                 }
252                 
253                 bool isTrackingViewState = false;
254                 protected bool IsTrackingViewState {
255                         get { return isTrackingViewState; }
256                 }
257
258                 // MSDN: The default implementation of the Evaluate method is to return 
259                 // a null reference (Nothing in Visual Basic) in all cases. 
260                 // Classes that derive from the Parameter class override the Evaluate method 
261                 // to return an updated parameter value. For example, the ControlParameter object 
262                 // returns the value of the control that it is bound to, while 
263                 // the QueryStringParameter object retrieves the current name/value pair from 
264                 // the HttpRequest object.
265                 protected virtual object Evaluate (HttpContext context, Control control)
266                 {
267                         return null;
268                 }
269
270                 internal void UpdateValue (HttpContext context, Control control)
271                 {
272                         object oldValue = ViewState ["ParameterValue"];
273
274                         object newValue = Evaluate (context, control);
275
276                         if (!object.Equals (oldValue, newValue)) {
277                                 ViewState ["ParameterValue"] = newValue;
278                                 OnParameterChanged ();
279                         }
280                 }
281
282                 internal object GetValue (HttpContext context, Control control)
283                 {
284                         UpdateValue (context, control);
285
286                         object value = ConvertValue (ViewState ["ParameterValue"]);
287                         if (value == null)
288                                 value = ConvertValue (DefaultValue);
289
290                         return value;
291                 }
292                 
293                 internal object ConvertValue (object val)
294                 {
295                         if (val == null) return null;
296                         if (ConvertEmptyStringToNull && val.Equals (string.Empty))
297                                 return null;
298                         return Type != TypeCode.Empty ? Convert.ChangeType (val, Type) : val;
299                 }
300                 
301                 protected internal virtual void SetDirty()
302                 {
303                         ViewState.SetDirty (true);
304                 }
305
306                 ParameterCollection _owner;
307                 internal void SetOwnerCollection (ParameterCollection own)
308                 {
309                         _owner = own;
310                 }
311         }
312 }
313 #endif
314