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 // 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 ()
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); }
137 if (DefaultValue != value) {
138 ViewState ["DefaultValue"] = value;
139 OnParameterChanged ();
144 [WebCategoryAttribute ("Parameter")]
145 [DefaultValueAttribute ("Input")]
146 [WebSysDescriptionAttribute ("Parameter's direction.")]
147 public ParameterDirection Direction
149 get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
151 if (Direction != value) {
152 ViewState ["Direction"] = value;
153 OnParameterChanged ();
159 [WebCategoryAttribute ("Parameter")]
160 [DefaultValueAttribute ("")]
161 [WebSysDescriptionAttribute ("Parameter's name.")]
165 string s = ViewState ["Name"] as string;
174 ViewState ["Name"] = value;
175 OnParameterChanged ();
180 [WebCategoryAttribute ("Parameter")]
181 [DefaultValueAttribute (true)]
182 [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
183 public bool ConvertEmptyStringToNull
185 get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
187 if (ConvertEmptyStringToNull != value) {
188 ViewState["ConvertEmptyStringToNull"] = value;
189 OnParameterChanged ();
196 get { return ViewState.GetInt ("Size", 0); }
199 ViewState["Size"] = value;
200 OnParameterChanged ();
205 [DefaultValueAttribute (TypeCode.Empty)]
206 [WebCategoryAttribute ("Parameter"),
207 WebSysDescriptionAttribute("Represents type of the parameter.")]
210 get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
214 ViewState ["Type"] = value;
215 OnParameterChanged ();
221 [BrowsableAttribute (false),
222 DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
223 protected StateBag ViewState {
225 if (viewState == null) {
226 viewState = new StateBag ();
227 if (IsTrackingViewState)
228 viewState.TrackViewState ();
234 bool isTrackingViewState = false;
235 protected bool IsTrackingViewState {
236 get { return isTrackingViewState; }
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)
251 internal void UpdateValue (HttpContext context, Control control)
253 object oldValue = ViewState ["ParameterValue"];
255 object newValue = Evaluate (context, control);
257 if (!object.Equals (oldValue, newValue)) {
258 ViewState ["ParameterValue"] = newValue;
259 OnParameterChanged ();
263 internal object GetValue (HttpContext context, Control control)
265 UpdateValue (context, control);
267 object value = ConvertValue (ViewState ["ParameterValue"]);
269 value = ConvertValue (DefaultValue);
274 internal object ConvertValue (object val)
276 if (val == null) return null;
277 if (ConvertEmptyStringToNull && val.Equals (string.Empty))
279 return Type != TypeCode.Empty ? Convert.ChangeType (val, Type) : val;
282 protected internal virtual void SetDirty()
284 ViewState.SetDirty (true);
288 private ParameterCollection _owner;
290 internal void SetOwnerCollection (ParameterCollection own)