DataControlField.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 (true);
  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)
  76. {
  77. if (HeaderText.Length > 0 || HeaderImageUrl.Length > 0) {
  78. if (sortingEnabled && SortExpression.Length > 0)
  79. cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, 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 internal DataControlField CloneField ()
  89. {
  90. DataControlField field = CreateField ();
  91. CopyProperties (field);
  92. return field;
  93. }
  94. protected abstract DataControlField CreateField ();
  95. protected virtual void CopyProperties (DataControlField newField)
  96. {
  97. newField.AccessibleHeaderText = AccessibleHeaderText;
  98. newField.ControlStyle.CopyFrom (ControlStyle);
  99. newField.FooterStyle.CopyFrom (FooterStyle);
  100. newField.FooterText = FooterText;
  101. newField.HeaderImageUrl = HeaderImageUrl;
  102. newField.HeaderStyle.CopyFrom (HeaderStyle);
  103. newField.HeaderText = HeaderText;
  104. newField.InsertVisible = InsertVisible;
  105. newField.ItemStyle.CopyFrom (ItemStyle);
  106. newField.ShowHeader = ShowHeader;
  107. newField.SortExpression = SortExpression;
  108. newField.Visible = Visible;
  109. }
  110. protected virtual void OnFieldChanged ()
  111. {
  112. if (FieldChanged != null)
  113. FieldChanged (this, EventArgs.Empty);
  114. }
  115. protected virtual void LoadViewState (object savedState)
  116. {
  117. if (savedState == null)
  118. return;
  119. object [] states = (object []) savedState;
  120. viewState.LoadViewState (states[0]);
  121. if (states[1] != null)
  122. ((IStateManager)controlStyle).LoadViewState (states[1]);
  123. if (states[2] != null)
  124. ((IStateManager)footerStyle).LoadViewState (states[2]);
  125. if (states[3] != null)
  126. ((IStateManager)headerStyle).LoadViewState (states[3]);
  127. if (states[4] != null)
  128. ((IStateManager)itemStyle).LoadViewState (states[4]);
  129. }
  130. protected virtual object SaveViewState()
  131. {
  132. object[] state = new object [5];
  133. state [0] = viewState.SaveViewState ();
  134. if (controlStyle != null)
  135. state [1] = ((IStateManager) controlStyle).SaveViewState ();
  136. if (footerStyle != null)
  137. state [2] = ((IStateManager) footerStyle).SaveViewState ();
  138. if (headerStyle != null)
  139. state [3] = ((IStateManager) headerStyle).SaveViewState ();
  140. if (itemStyle != null)
  141. state [4] = ((IStateManager) itemStyle).SaveViewState ();
  142. if (state [0] == null && state [1] == null && state [2] == null &&
  143. state [3] == null && state [4] == null)
  144. return null;
  145. return state;
  146. }
  147. protected virtual void TrackViewState()
  148. {
  149. if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
  150. if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
  151. if (headerStyle != null) ((IStateManager) headerStyle).TrackViewState ();
  152. if (itemStyle != null) ((IStateManager) itemStyle).TrackViewState ();
  153. viewState.TrackViewState ();
  154. tracking = true;
  155. }
  156. public virtual void ValidateSupportsCallback ()
  157. {
  158. throw new NotSupportedException ("Callback not supported");
  159. }
  160. void IStateManager.LoadViewState(object savedState)
  161. {
  162. LoadViewState(savedState);
  163. }
  164. object IStateManager.SaveViewState()
  165. {
  166. return SaveViewState();
  167. }
  168. void IStateManager.TrackViewState()
  169. {
  170. TrackViewState();
  171. }
  172. internal Exception GetNotSupportedPropException (string propName)
  173. {
  174. return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name);
  175. }
  176. [MonoTODO ("Render this")]
  177. [DefaultValueAttribute ("")]
  178. [LocalizableAttribute (true)]
  179. [WebCategoryAttribute ("Accessibility")]
  180. public virtual string AccessibleHeaderText {
  181. get {
  182. object val = viewState ["accessibleHeaderText"];
  183. return val != null ? (string) val : "";
  184. }
  185. set {
  186. viewState ["accessibleHeaderText"] = value;
  187. OnFieldChanged ();
  188. }
  189. }
  190. protected Control Control {
  191. get { return control; }
  192. }
  193. [WebCategoryAttribute ("Styles")]
  194. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  195. [DefaultValueAttribute (null)]
  196. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  197. public Style ControlStyle {
  198. get {
  199. if (controlStyle == null) {
  200. controlStyle = new Style ();
  201. if (IsTrackingViewState)
  202. controlStyle.TrackViewState();
  203. }
  204. return controlStyle;
  205. }
  206. }
  207. protected bool DesignMode {
  208. get { return control != null && control.Site != null ? control.Site.DesignMode : false; }
  209. }
  210. [DefaultValueAttribute (null)]
  211. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  212. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  213. [WebCategoryAttribute ("Styles")]
  214. public TableItemStyle FooterStyle {
  215. get {
  216. if (footerStyle == null) {
  217. footerStyle = new TableItemStyle ();
  218. if (IsTrackingViewState)
  219. footerStyle.TrackViewState();
  220. }
  221. return footerStyle;
  222. }
  223. }
  224. [LocalizableAttribute (true)]
  225. [WebCategoryAttribute ("Appearance")]
  226. [DefaultValue ("")]
  227. public virtual string FooterText {
  228. get {
  229. object val = viewState ["footerText"];
  230. return val != null ? (string) val : "";
  231. }
  232. set {
  233. viewState ["footerText"] = value;
  234. OnFieldChanged ();
  235. }
  236. }
  237. [UrlPropertyAttribute]
  238. [DefaultValueAttribute ("")]
  239. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  240. [WebCategoryAttribute ("Appearance")]
  241. public virtual string HeaderImageUrl {
  242. get {
  243. object val = viewState ["headerImageUrl"];
  244. return val != null ? (string) val : "";
  245. }
  246. set {
  247. viewState ["headerImageUrl"] = value;
  248. OnFieldChanged ();
  249. }
  250. }
  251. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  252. [WebCategoryAttribute ("Styles")]
  253. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  254. [DefaultValueAttribute (null)]
  255. public TableItemStyle HeaderStyle {
  256. get {
  257. if (headerStyle == null) {
  258. headerStyle = new TableItemStyle ();
  259. if (IsTrackingViewState)
  260. headerStyle.TrackViewState();
  261. }
  262. return headerStyle;
  263. }
  264. }
  265. [DefaultValueAttribute ("")]
  266. [LocalizableAttribute (true)]
  267. [WebCategoryAttribute ("Appearance")]
  268. public virtual string HeaderText {
  269. get {
  270. object val = viewState ["headerText"];
  271. return val != null ? (string) val : "";
  272. }
  273. set {
  274. viewState ["headerText"] = value;
  275. OnFieldChanged ();
  276. }
  277. }
  278. [WebCategoryAttribute ("Behavior")]
  279. [DefaultValueAttribute (true)]
  280. public virtual bool InsertVisible {
  281. get {
  282. object val = viewState ["InsertVisible"];
  283. return val != null ? (bool) val : true;
  284. }
  285. set {
  286. viewState ["InsertVisible"] = value;
  287. OnFieldChanged ();
  288. }
  289. }
  290. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  291. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  292. [WebCategoryAttribute ("Styles")]
  293. [DefaultValueAttribute (null)]
  294. public TableItemStyle ItemStyle {
  295. get {
  296. if (itemStyle == null) {
  297. itemStyle = new TableItemStyle ();
  298. if (IsTrackingViewState)
  299. itemStyle.TrackViewState();
  300. }
  301. return itemStyle;
  302. }
  303. }
  304. [WebCategoryAttribute ("Behavior")]
  305. [DefaultValueAttribute (true)]
  306. public virtual bool ShowHeader {
  307. get {
  308. object val = viewState ["showHeader"];
  309. return val != null ? (bool) val : true;
  310. }
  311. set {
  312. viewState ["showHeader"] = value;
  313. OnFieldChanged ();
  314. }
  315. }
  316. [DefaultValueAttribute ("")]
  317. // [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  318. [WebCategoryAttribute ("Behavior")]
  319. public virtual string SortExpression {
  320. get {
  321. object val = viewState ["sortExpression"];
  322. return val != null ? (string) val : "";
  323. }
  324. set {
  325. viewState ["sortExpression"] = value;
  326. OnFieldChanged ();
  327. }
  328. }
  329. [WebCategoryAttribute ("Behavior")]
  330. [DefaultValueAttribute (true)]
  331. public bool Visible {
  332. get {
  333. object val = viewState ["visible"];
  334. return val != null ? (bool) val : true;
  335. }
  336. set {
  337. viewState ["visible"] = value;
  338. OnFieldChanged ();
  339. }
  340. }
  341. protected bool IsTrackingViewState
  342. {
  343. get { return tracking; }
  344. }
  345. bool IStateManager.IsTrackingViewState
  346. {
  347. get { return IsTrackingViewState; }
  348. }
  349. object IDataSourceViewSchemaAccessor.DataSourceViewSchema {
  350. get { return viewState ["dataSourceViewSchema"]; }
  351. set {
  352. viewState ["dataSourceViewSchema"] = value;
  353. }
  354. }
  355. internal event EventHandler FieldChanged;
  356. }
  357. }
  358. #endif