DataControlField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // System.Web.UI.WebControls.DataControlField.cs
  3. //
  4. // Authors:
  5. // Sanjay Gupta ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2004 Novell, Inc. (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Web.UI;
  34. using System.ComponentModel;
  35. using System.Security.Permissions;
  36. namespace System.Web.UI.WebControls {
  37. [DefaultPropertyAttribute ("HeaderText")]
  38. [TypeConverterAttribute (typeof(ExpandableObjectConverter))]
  39. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. public abstract class DataControlField : IStateManager, IDataSourceViewSchemaAccessor
  42. {
  43. bool tracking = false;
  44. StateBag viewState;
  45. Control control;
  46. Style controlStyle;
  47. TableItemStyle footerStyle;
  48. TableItemStyle headerStyle;
  49. TableItemStyle itemStyle;
  50. bool sortingEnabled;
  51. protected DataControlField()
  52. {
  53. viewState = new StateBag ();
  54. }
  55. internal void SetDirty ()
  56. {
  57. viewState.SetDirty ();
  58. }
  59. protected StateBag ViewState {
  60. get { return viewState; }
  61. }
  62. public virtual void ExtractValuesFromCell (IOrderedDictionary dictionary,
  63. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  64. {
  65. }
  66. public virtual bool Initialize (bool sortingEnabled, Control control)
  67. {
  68. this.sortingEnabled = sortingEnabled;
  69. this.control = control;
  70. return true;
  71. }
  72. public virtual void InitializeCell (DataControlFieldCell cell,
  73. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  74. {
  75. if (cellType == DataControlCellType.Header && ShowHeader)
  76. {
  77. if (HeaderText.Length > 0 || HeaderImageUrl.Length > 0) {
  78. if (sortingEnabled && SortExpression.Length > 0)
  79. cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, "Sort", SortExpression, true));
  80. else
  81. cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, string.Empty, string.Empty, true));
  82. }
  83. }
  84. else if (cellType == DataControlCellType.Footer) {
  85. cell.Text = FooterText;
  86. }
  87. }
  88. protected virtual void OnFieldChanged ()
  89. {
  90. if (FieldChanged != null)
  91. FieldChanged (this, EventArgs.Empty);
  92. }
  93. protected virtual void LoadViewState (object savedState)
  94. {
  95. if (savedState == null)
  96. return;
  97. object [] states = (object []) savedState;
  98. viewState.LoadViewState (states[0]);
  99. if (states[1] != null)
  100. ((IStateManager)controlStyle).LoadViewState (states[1]);
  101. if (states[2] != null)
  102. ((IStateManager)footerStyle).LoadViewState (states[2]);
  103. if (states[3] != null)
  104. ((IStateManager)headerStyle).LoadViewState (states[3]);
  105. if (states[4] != null)
  106. ((IStateManager)itemStyle).LoadViewState (states[4]);
  107. }
  108. protected virtual object SaveViewState()
  109. {
  110. object[] state = new object [5];
  111. state [0] = viewState.SaveViewState ();
  112. if (controlStyle != null)
  113. state [1] = ((IStateManager) controlStyle).SaveViewState ();
  114. if (footerStyle != null)
  115. state [2] = ((IStateManager) footerStyle).SaveViewState ();
  116. if (headerStyle != null)
  117. state [3] = ((IStateManager) headerStyle).SaveViewState ();
  118. if (itemStyle != null)
  119. state [4] = ((IStateManager) itemStyle).SaveViewState ();
  120. if (state [0] == null && state [1] == null && state [2] == null &&
  121. state [3] == null && state [4] == null)
  122. return null;
  123. return state;
  124. }
  125. protected virtual void TrackViewState()
  126. {
  127. if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
  128. if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
  129. if (headerStyle != null) ((IStateManager) headerStyle).TrackViewState ();
  130. if (itemStyle != null) ((IStateManager) itemStyle).TrackViewState ();
  131. viewState.TrackViewState ();
  132. tracking = true;
  133. }
  134. public virtual void ValidateSupportsCallback ()
  135. {
  136. throw new NotSupportedException ("Callback not supported");
  137. }
  138. void IStateManager.LoadViewState(object savedState)
  139. {
  140. LoadViewState(savedState);
  141. }
  142. object IStateManager.SaveViewState()
  143. {
  144. return SaveViewState();
  145. }
  146. void IStateManager.TrackViewState()
  147. {
  148. TrackViewState();
  149. }
  150. internal Exception GetNotSupportedPropException (string propName)
  151. {
  152. return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name);
  153. }
  154. [MonoTODO ("Render this")]
  155. [DefaultValueAttribute ("")]
  156. [LocalizableAttribute (true)]
  157. [WebCategoryAttribute ("Accessibility")]
  158. public virtual string AccessibleHeaderText {
  159. get {
  160. object val = viewState ["accessibleHeaderText"];
  161. return val != null ? (string) val : "";
  162. }
  163. set {
  164. viewState ["accessibleHeaderText"] = value;
  165. OnFieldChanged ();
  166. }
  167. }
  168. protected Control Control {
  169. get { return control; }
  170. }
  171. [WebCategoryAttribute ("Styles")]
  172. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  173. [DefaultValueAttribute (null)]
  174. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  175. public virtual Style ControlStyle {
  176. get {
  177. if (controlStyle == null) {
  178. controlStyle = new Style ();
  179. if (IsTrackingViewState)
  180. controlStyle.TrackViewState();
  181. }
  182. return controlStyle;
  183. }
  184. }
  185. protected bool DesignMode {
  186. get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
  187. }
  188. [DefaultValueAttribute (null)]
  189. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  190. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  191. [WebCategoryAttribute ("Styles")]
  192. public virtual TableItemStyle FooterStyle {
  193. get {
  194. if (footerStyle == null) {
  195. footerStyle = new TableItemStyle ();
  196. if (IsTrackingViewState)
  197. footerStyle.TrackViewState();
  198. }
  199. return footerStyle;
  200. }
  201. }
  202. [LocalizableAttribute (true)]
  203. [WebCategoryAttribute ("Appearance")]
  204. [DefaultValue ("")]
  205. public virtual string FooterText {
  206. get {
  207. object val = viewState ["footerText"];
  208. return val != null ? (string) val : "";
  209. }
  210. set {
  211. viewState ["footerText"] = value;
  212. OnFieldChanged ();
  213. }
  214. }
  215. [UrlPropertyAttribute]
  216. [DefaultValueAttribute ("")]
  217. [EditorAttribute ("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  218. [WebCategoryAttribute ("Appearance")]
  219. public virtual string HeaderImageUrl {
  220. get {
  221. object val = viewState ["headerImageUrl"];
  222. return val != null ? (string) val : "";
  223. }
  224. set {
  225. viewState ["headerImageUrl"] = value;
  226. OnFieldChanged ();
  227. }
  228. }
  229. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  230. [WebCategoryAttribute ("Styles")]
  231. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  232. [DefaultValueAttribute (null)]
  233. public virtual TableItemStyle HeaderStyle {
  234. get {
  235. if (headerStyle == null) {
  236. headerStyle = new TableItemStyle ();
  237. if (IsTrackingViewState)
  238. headerStyle.TrackViewState();
  239. }
  240. return headerStyle;
  241. }
  242. }
  243. [DefaultValueAttribute ("")]
  244. [LocalizableAttribute (true)]
  245. [WebCategoryAttribute ("Appearance")]
  246. public virtual string HeaderText {
  247. get {
  248. object val = viewState ["headerText"];
  249. return val != null ? (string) val : "";
  250. }
  251. set {
  252. viewState ["headerText"] = value;
  253. OnFieldChanged ();
  254. }
  255. }
  256. [WebCategoryAttribute ("Behavior")]
  257. [DefaultValueAttribute (true)]
  258. public virtual bool InsertVisible {
  259. get {
  260. object val = viewState ["InsertVisible"];
  261. return val != null ? (bool) val : true;
  262. }
  263. set {
  264. viewState ["InsertVisible"] = value;
  265. OnFieldChanged ();
  266. }
  267. }
  268. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  269. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  270. [WebCategoryAttribute ("Styles")]
  271. [DefaultValueAttribute (null)]
  272. public virtual TableItemStyle ItemStyle {
  273. get {
  274. if (itemStyle == null) {
  275. itemStyle = new TableItemStyle ();
  276. if (IsTrackingViewState)
  277. itemStyle.TrackViewState();
  278. }
  279. return itemStyle;
  280. }
  281. }
  282. [WebCategoryAttribute ("Behavior")]
  283. [DefaultValueAttribute (true)]
  284. public virtual bool ShowHeader {
  285. get {
  286. object val = viewState ["showHeader"];
  287. return val != null ? (bool) val : true;
  288. }
  289. set {
  290. viewState ["showHeader"] = value;
  291. OnFieldChanged ();
  292. }
  293. }
  294. [DefaultValueAttribute ("")]
  295. // [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  296. [WebCategoryAttribute ("Behavior")]
  297. public virtual string SortExpression {
  298. get {
  299. object val = viewState ["sortExpression"];
  300. return val != null ? (string) val : "";
  301. }
  302. set {
  303. viewState ["sortExpression"] = value;
  304. OnFieldChanged ();
  305. }
  306. }
  307. [WebCategoryAttribute ("Behavior")]
  308. [DefaultValueAttribute (true)]
  309. public bool Visible {
  310. get {
  311. object val = viewState ["visible"];
  312. return val != null ? (bool) val : true;
  313. }
  314. set {
  315. viewState ["visible"] = value;
  316. OnFieldChanged ();
  317. }
  318. }
  319. protected bool IsTrackingViewState
  320. {
  321. get { return tracking; }
  322. }
  323. bool IStateManager.IsTrackingViewState
  324. {
  325. get { return IsTrackingViewState; }
  326. }
  327. object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
  328. get { return viewState ["dataSourceViewSchema"]; }
  329. set {
  330. viewState ["dataSourceViewSchema"] = value;
  331. }
  332. }
  333. internal event EventHandler FieldChanged;
  334. }
  335. }
  336. #endif