2009-06-12 Bill Holmes <billholmes54@gmail.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                 [DefaultValue (0)]
195                 public int Size {
196                         get { return ViewState.GetInt ("Size", 0); }
197                         set {
198                                 if (Size != value) {
199                                         ViewState["Size"] = value;
200                                         OnParameterChanged ();
201                                 }
202                         }
203                 }
204
205                 [DefaultValueAttribute (TypeCode.Empty)]
206                 [WebCategoryAttribute ("Parameter"), 
207                 WebSysDescriptionAttribute("Represents type of the parameter.")]
208                 public TypeCode Type
209                 {
210                         get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
211                         set {
212                                 
213                                 if (Type != value) {
214                                         ViewState ["Type"] = value;
215                                         OnParameterChanged ();
216                                 }
217                         }
218                 }
219                 
220                 StateBag viewState;
221                 [BrowsableAttribute (false), 
222                 DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
223                 protected StateBag ViewState {
224                         get {
225                                 if (viewState == null) {
226                                         viewState = new StateBag ();
227                                         if (IsTrackingViewState)
228                                                 viewState.TrackViewState ();
229                                 }
230                                 return viewState;
231                         }
232                 }
233                 
234                 bool isTrackingViewState = false;
235                 protected bool IsTrackingViewState {
236                         get { return isTrackingViewState; }
237                 }
238
239                 // MSDN: The default implementation of the Evaluate method is to return 
240                 // a null reference (Nothing in Visual Basic) in all cases. 
241                 // Classes that derive from the Parameter class override the Evaluate method 
242                 // to return an updated parameter value. For example, the ControlParameter object 
243                 // returns the value of the control that it is bound to, while 
244                 // the QueryStringParameter object retrieves the current name/value pair from 
245                 // the HttpRequest object.
246                 protected virtual object Evaluate (HttpContext context, Control control)
247                 {
248                         return null;
249                 }
250
251                 internal void UpdateValue (HttpContext context, Control control)
252                 {
253                         object oldValue = ViewState ["ParameterValue"];
254
255                         object newValue = Evaluate (context, control);
256
257                         if (!object.Equals (oldValue, newValue)) {
258                                 ViewState ["ParameterValue"] = newValue;
259                                 OnParameterChanged ();
260                         }
261                 }
262
263                 internal object GetValue (HttpContext context, Control control)
264                 {
265                         UpdateValue (context, control);
266
267                         object value = ConvertValue (ViewState ["ParameterValue"]);
268                         if (value == null)
269                                 value = ConvertValue (DefaultValue);
270
271                         return value;
272                 }
273                 
274                 internal object ConvertValue (object val)
275                 {
276                         if (val == null) return null;
277                         if (ConvertEmptyStringToNull && val.Equals (string.Empty))
278                                 return null;
279                         return Type != TypeCode.Empty ? Convert.ChangeType (val, Type) : val;
280                 }
281                 
282                 protected internal virtual void SetDirty()
283                 {
284                         ViewState.SetDirty (true);
285                 }
286
287                 ParameterCollection _owner;
288                 internal void SetOwnerCollection (ParameterCollection own)
289                 {
290                         _owner = own;
291                 }
292         }
293 }
294 #endif
295