Parameter.cs 7.2 KB

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