Parameter.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. if (savedState == null)
  75. return;
  76. ViewState.LoadViewState (savedState);
  77. }
  78. protected virtual object SaveViewState ()
  79. {
  80. if (viewState == null)
  81. return null;
  82. return viewState.SaveViewState ();
  83. }
  84. protected virtual void TrackViewState ()
  85. {
  86. isTrackingViewState = true;
  87. if (viewState != null)
  88. viewState.TrackViewState ();
  89. }
  90. object ICloneable.Clone ()
  91. {
  92. return this.Clone ();
  93. }
  94. void IStateManager.LoadViewState (object savedState)
  95. {
  96. this.LoadViewState (savedState);
  97. }
  98. object IStateManager.SaveViewState ()
  99. {
  100. return this.SaveViewState ();
  101. }
  102. void IStateManager.TrackViewState ()
  103. {
  104. this.TrackViewState ();
  105. }
  106. bool IStateManager.IsTrackingViewState {
  107. get { return this.IsTrackingViewState; }
  108. }
  109. [MonoTODO]
  110. public override string ToString ()
  111. {
  112. return base.ToString ();
  113. }
  114. [WebCategoryAttribute ("Parameter"), DefaultValueAttribute (""),
  115. WebSysDescriptionAttribute ("Default value to be used in case value is null.") ]
  116. public string DefaultValue {
  117. get {
  118. return ViewState ["DefaultValue"] as string;
  119. }
  120. set {
  121. if (DefaultValue != value) {
  122. ViewState ["DefaultValue"] = value;
  123. OnParameterChanged ();
  124. }
  125. }
  126. }
  127. [WebCategoryAttribute ("Parameter"), DefaultValueAttribute ("Input"),
  128. WebSysDescriptionAttribute ("Parameter's direction.")]
  129. public ParameterDirection Direction
  130. {
  131. get {
  132. object o = ViewState ["Direction"];
  133. if (o != null)
  134. return (ParameterDirection) o;
  135. return ParameterDirection.Input;
  136. }
  137. set {
  138. if (Direction != value) {
  139. ViewState ["Direction"] = value;
  140. OnParameterChanged ();
  141. }
  142. }
  143. }
  144. [WebCategoryAttribute ("Parameter"), DefaultValueAttribute (""),
  145. WebSysDescriptionAttribute ("Parameter's name.")]
  146. public string Name
  147. {
  148. get {
  149. string s = ViewState ["Name"] as string;
  150. if (s != null)
  151. return s;
  152. return "";
  153. }
  154. set {
  155. if (Name != value) {
  156. ViewState ["Name"] = value;
  157. OnParameterChanged ();
  158. }
  159. }
  160. }
  161. [WebCategoryAttribute ("Parameter"), DefaultValueAttribute (true),
  162. WebSysDescriptionAttribute ("Checks whether an empty string is treated as a null value.")]
  163. public bool ConvertEmptyStringToNull
  164. {
  165. get {
  166. object o = ViewState["ConvertEmptyStringToNull"];
  167. if (o != null)
  168. return (bool) o;
  169. return false;
  170. }
  171. set {
  172. if (ConvertEmptyStringToNull != value) {
  173. ViewState["ConvertEmptyStringToNull"] = value;
  174. OnParameterChanged ();
  175. }
  176. }
  177. }
  178. [DefaultValueAttribute (TypeCode.Object)]
  179. [WebCategoryAttribute ("Parameter"),
  180. WebSysDescriptionAttribute("Represents type of the parameter.")]
  181. public TypeCode Type
  182. {
  183. get {
  184. object o = ViewState ["Type"];
  185. if (o != null)
  186. return (TypeCode) o;
  187. return TypeCode.Object;
  188. }
  189. set {
  190. if (Type != value) {
  191. ViewState ["Type"] = value;
  192. OnParameterChanged ();
  193. }
  194. }
  195. }
  196. StateBag viewState;
  197. [BrowsableAttribute (false),
  198. DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  199. protected StateBag ViewState {
  200. get {
  201. if (viewState == null) {
  202. viewState = new StateBag ();
  203. if (IsTrackingViewState)
  204. viewState.TrackViewState ();
  205. }
  206. return viewState;
  207. }
  208. }
  209. bool isTrackingViewState = false;
  210. protected bool IsTrackingViewState {
  211. get { return isTrackingViewState; }
  212. }
  213. protected virtual object Evaluate (HttpContext context, Control control)
  214. {
  215. return this.DefaultValue;
  216. }
  217. internal object GetValue (HttpContext context, Control control)
  218. {
  219. return Evaluate (context, control);
  220. }
  221. protected internal virtual void SetDirty()
  222. {
  223. this.SaveViewState();
  224. }
  225. private ParameterCollection _owner;
  226. internal void SetOwnerCollection (ParameterCollection own)
  227. {
  228. _owner = own;
  229. }
  230. internal object ParameterValue {
  231. get {
  232. object param = ViewState["ParameterValue"];
  233. //FIXME: need to do some null string checking magic with TreatEmptyStringAsNull here
  234. if (param == null)
  235. {
  236. param = DefaultValue;
  237. if (param == null)
  238. {
  239. return null;
  240. }
  241. }
  242. return Convert.ChangeType (param, Type);
  243. }
  244. }
  245. }
  246. }
  247. #endif