2 // System.Web.UI.WebControls.Parameter
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) 2004 Novell, Inc. (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Collections.Specialized;
38 using System.ComponentModel;
40 namespace System.Web.UI.WebControls {
41 [DefaultPropertyAttribute ("DefaultValue")]
42 public class Parameter : ICloneable, IStateManager {
44 public Parameter () : base ()
48 protected Parameter (Parameter original)
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;
57 public Parameter (string name)
62 public Parameter(string name, TypeCode type) : this (name)
67 public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
69 this.DefaultValue = defaultValue;
72 protected virtual Parameter Clone ()
74 return new Parameter (this);
77 protected void OnParameterChanged ()
80 _owner.CallOnParameterChanged ();
83 protected virtual void LoadViewState (object savedState)
85 ViewState.LoadViewState (savedState);
88 protected virtual object SaveViewState ()
90 return ViewState.SaveViewState ();
93 protected virtual void TrackViewState ()
95 isTrackingViewState = true;
96 if (viewState != null)
97 viewState.TrackViewState ();
100 object ICloneable.Clone ()
102 return this.Clone ();
105 void IStateManager.LoadViewState (object savedState)
107 this.LoadViewState (savedState);
110 object IStateManager.SaveViewState ()
112 return this.SaveViewState ();
115 void IStateManager.TrackViewState ()
117 this.TrackViewState ();
120 bool IStateManager.IsTrackingViewState {
121 get { return this.IsTrackingViewState; }
124 public override string ToString ()
126 object o = GetValue (HttpContext.Current, null);
127 if (o != null) return o.ToString();
131 [WebCategoryAttribute ("Parameter")]
132 [DefaultValueAttribute (null)]
133 [WebSysDescriptionAttribute ("Default value to be used in case value is null.")]
134 public string DefaultValue {
135 get { return ViewState.GetString ("DefaultValue", null); }
138 if (DefaultValue != value) {
139 ViewState ["DefaultValue"] = value;
140 OnParameterChanged ();
145 [WebCategoryAttribute ("Parameter")]
146 [DefaultValueAttribute ("Input")]
147 [WebSysDescriptionAttribute ("Parameter's direction.")]
148 public ParameterDirection Direction
150 get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
152 if (Direction != value) {
153 ViewState ["Direction"] = value;
154 OnParameterChanged ();
160 [WebCategoryAttribute ("Parameter")]
161 [DefaultValueAttribute ("")]
162 [WebSysDescriptionAttribute ("Parameter's name.")]
166 string s = ViewState ["Name"] as string;
175 ViewState ["Name"] = value;
176 OnParameterChanged ();
181 [WebCategoryAttribute ("Parameter")]
182 [DefaultValueAttribute (true)]
183 [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
184 public bool ConvertEmptyStringToNull
186 get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
188 if (ConvertEmptyStringToNull != value) {
189 ViewState["ConvertEmptyStringToNull"] = value;
190 OnParameterChanged ();
197 get { return ViewState.GetInt ("Size", 0); }
200 ViewState["Size"] = value;
201 OnParameterChanged ();
206 [DefaultValueAttribute (TypeCode.Empty)]
207 [WebCategoryAttribute ("Parameter"),
208 WebSysDescriptionAttribute("Represents type of the parameter.")]
211 get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
215 ViewState ["Type"] = value;
216 OnParameterChanged ();
222 [BrowsableAttribute (false),
223 DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
224 protected StateBag ViewState {
226 if (viewState == null) {
227 viewState = new StateBag ();
228 if (IsTrackingViewState)
229 viewState.TrackViewState ();
235 bool isTrackingViewState = false;
236 protected bool IsTrackingViewState {
237 get { return isTrackingViewState; }
240 protected virtual object Evaluate (HttpContext context, Control control)
242 return this.DefaultValue;
245 internal object GetValue (HttpContext context, Control control)
247 object oldValue = ViewState ["ParameterValue"];
249 object newValue = ConvertValue (Evaluate (context, control));
250 if (newValue == null)
251 newValue = ConvertValue (DefaultValue);
253 if (!object.Equals (oldValue, newValue)) {
254 ViewState ["ParameterValue"] = newValue;
255 OnParameterChanged ();
260 object ConvertValue (object val)
262 if (val == null) return null;
263 if (ConvertEmptyStringToNull && val.Equals (string.Empty))
265 return Convert.ChangeType (val, Type);
268 protected internal virtual void SetDirty()
270 ViewState.SetDirty (true);
274 private ParameterCollection _owner;
276 internal void SetOwnerCollection (ParameterCollection own)