Parameter.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // System.Web.UI.WebControls.Parameter
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Text;
  33. using System.Data;
  34. namespace System.Web.UI.WebControls {
  35. public class Parameter : ICloneable, IStateManager {
  36. public Parameter () : base ()
  37. {
  38. }
  39. protected Parameter (Parameter original)
  40. {
  41. this.DefaultValue = original.DefaultValue;
  42. this.Direction = original.Direction;
  43. this.TreatEmptyStringAsNull = original.TreatEmptyStringAsNull;
  44. this.Type = original.Type;
  45. this.Name = original.Name;
  46. }
  47. public Parameter (string name)
  48. {
  49. this.Name = name;
  50. }
  51. public Parameter(string name, TypeCode type) : this (name)
  52. {
  53. this.Type = type;
  54. }
  55. public Parameter (string name, TypeCode type, string defaultValue) : this (name, type)
  56. {
  57. this.DefaultValue = defaultValue;
  58. }
  59. protected virtual Parameter Clone ()
  60. {
  61. return new Parameter (this);
  62. }
  63. protected virtual object Evaluate (Control control)
  64. {
  65. return this.DefaultValue;
  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. public string DefaultValue {
  115. get {
  116. return ViewState ["DefaultValue"] as string;
  117. }
  118. set {
  119. if (DefaultValue != value) {
  120. ViewState ["DefaultValue"] = value;
  121. OnParameterChanged ();
  122. }
  123. }
  124. }
  125. public ParameterDirection Direction {
  126. get {
  127. object o = ViewState ["Direction"];
  128. if (o != null)
  129. return (ParameterDirection) o;
  130. return ParameterDirection.Input;
  131. }
  132. set {
  133. if (Direction != value) {
  134. ViewState ["Direction"] = value;
  135. OnParameterChanged ();
  136. }
  137. }
  138. }
  139. public string Name {
  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. public bool TreatEmptyStringAsNull {
  154. get {
  155. object o = ViewState ["TreatEmptyStringAsNull"];
  156. if (o != null)
  157. return (bool) o;
  158. return false;
  159. }
  160. set {
  161. if (TreatEmptyStringAsNull != value) {
  162. ViewState ["TreatEmptyStringAsNull"] = value;
  163. OnParameterChanged ();
  164. }
  165. }
  166. }
  167. public TypeCode Type {
  168. get {
  169. object o = ViewState ["Type"];
  170. if (o != null)
  171. return (TypeCode) o;
  172. return TypeCode.Object;
  173. }
  174. set {
  175. if (Type != value) {
  176. ViewState ["Type"] = value;
  177. OnParameterChanged ();
  178. }
  179. }
  180. }
  181. StateBag viewState;
  182. protected StateBag ViewState {
  183. get {
  184. if (viewState == null) {
  185. viewState = new StateBag ();
  186. if (IsTrackingViewState)
  187. viewState.TrackViewState ();
  188. }
  189. return viewState;
  190. }
  191. }
  192. bool isTrackingViewState = false;
  193. protected bool IsTrackingViewState {
  194. get { return isTrackingViewState; }
  195. }
  196. private ParameterCollection _owner;
  197. internal void SetOwnerCollection (ParameterCollection own)
  198. {
  199. _owner = own;
  200. }
  201. internal object ParameterValue {
  202. get {
  203. object param = ViewState["ParameterValue"];
  204. //FIXME: need to do some null string checking magic with TreatEmptyStringAsNull here
  205. if (param == null)
  206. {
  207. param = DefaultValue;
  208. if (param == null)
  209. {
  210. return null;
  211. }
  212. }
  213. return Convert.ChangeType (param, Type);
  214. }
  215. }
  216. }
  217. }
  218. #endif