Parameter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. public override string ToString ()
  106. {
  107. object o = GetValue (HttpContext.Current, null);
  108. if (o != null) return o.ToString();
  109. return "";
  110. }
  111. [WebCategoryAttribute ("Parameter")]
  112. [DefaultValueAttribute (null)]
  113. [WebSysDescriptionAttribute ("Default value to be used in case value is null.")]
  114. public string DefaultValue {
  115. get { return ViewState.GetString ("DefaultValue", null); }
  116. set {
  117. if (DefaultValue != value) {
  118. ViewState ["DefaultValue"] = value;
  119. OnParameterChanged ();
  120. }
  121. }
  122. }
  123. [WebCategoryAttribute ("Parameter")]
  124. [DefaultValueAttribute ("Input")]
  125. [WebSysDescriptionAttribute ("Parameter's direction.")]
  126. public ParameterDirection Direction
  127. {
  128. get { return (ParameterDirection) ViewState.GetInt ("Direction", (int)ParameterDirection.Input); }
  129. set {
  130. if (Direction != value) {
  131. ViewState ["Direction"] = value;
  132. OnParameterChanged ();
  133. }
  134. }
  135. }
  136. [WebCategoryAttribute ("Parameter")]
  137. [DefaultValueAttribute ("")]
  138. [WebSysDescriptionAttribute ("Parameter's name.")]
  139. public string Name
  140. {
  141. get {
  142. string s = ViewState ["Name"] as string;
  143. if (s != null)
  144. return s;
  145. return "";
  146. }
  147. set {
  148. if (Name != value) {
  149. ViewState ["Name"] = value;
  150. OnParameterChanged ();
  151. }
  152. }
  153. }
  154. [WebCategoryAttribute ("Parameter")]
  155. [DefaultValueAttribute (true)]
  156. [WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
  157. public bool ConvertEmptyStringToNull
  158. {
  159. get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
  160. set {
  161. if (ConvertEmptyStringToNull != value) {
  162. ViewState["ConvertEmptyStringToNull"] = value;
  163. OnParameterChanged ();
  164. }
  165. }
  166. }
  167. [DefaultValue (0)]
  168. public int Size {
  169. get { return ViewState.GetInt ("Size", 0); }
  170. set {
  171. if (Size != value) {
  172. ViewState["Size"] = value;
  173. OnParameterChanged ();
  174. }
  175. }
  176. }
  177. [DefaultValueAttribute (TypeCode.Empty)]
  178. [WebCategoryAttribute ("Parameter"),
  179. WebSysDescriptionAttribute("Represents type of the parameter.")]
  180. public TypeCode Type
  181. {
  182. get { return (TypeCode) ViewState.GetInt ("Type", (int)TypeCode.Empty); }
  183. set {
  184. if (Type != value) {
  185. ViewState ["Type"] = value;
  186. OnParameterChanged ();
  187. }
  188. }
  189. }
  190. StateBag viewState;
  191. [BrowsableAttribute (false),
  192. DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  193. protected StateBag ViewState {
  194. get {
  195. if (viewState == null) {
  196. viewState = new StateBag ();
  197. if (IsTrackingViewState)
  198. viewState.TrackViewState ();
  199. }
  200. return viewState;
  201. }
  202. }
  203. bool isTrackingViewState = false;
  204. protected bool IsTrackingViewState {
  205. get { return isTrackingViewState; }
  206. }
  207. protected virtual object Evaluate (HttpContext context, Control control)
  208. {
  209. return this.DefaultValue;
  210. }
  211. internal object GetValue (HttpContext context, Control control)
  212. {
  213. object oldValue = ViewState ["ParameterValue"];
  214. object newValue = ConvertValue (Evaluate (context, control));
  215. if (newValue == null)
  216. newValue = ConvertValue (DefaultValue);
  217. if (!object.Equals (oldValue, newValue)) {
  218. ViewState ["ParameterValue"] = newValue;
  219. OnParameterChanged ();
  220. }
  221. return newValue;
  222. }
  223. object ConvertValue (object val)
  224. {
  225. if (val == null) return null;
  226. if (ConvertEmptyStringToNull && val.Equals (string.Empty))
  227. return null;
  228. return Convert.ChangeType (val, Type);
  229. }
  230. protected internal virtual void SetDirty()
  231. {
  232. ViewState.SetDirty (true);
  233. }
  234. private ParameterCollection _owner;
  235. internal void SetOwnerCollection (ParameterCollection own)
  236. {
  237. _owner = own;
  238. }
  239. }
  240. }
  241. #endif