DynamicField.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // DynamicField.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. // Marek Habersack <[email protected]>
  7. //
  8. // Copyright (C) 2008-2009 Novell Inc. http://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. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Collections.Specialized;
  34. using System.Globalization;
  35. using System.Security.Permissions;
  36. using System.Security.Principal;
  37. using System.Web.Caching;
  38. using System.Web.UI;
  39. using System.Web.UI.WebControls;
  40. namespace System.Web.DynamicData
  41. {
  42. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  44. public class DynamicField : DataControlField, IAttributeAccessor, IFieldFormattingOptions
  45. {
  46. MetaColumn myColumn;
  47. Dictionary <string, string> attributes;
  48. public DynamicField ()
  49. {
  50. DataFormatString = String.Empty;
  51. HtmlEncode = true;
  52. NullDisplayText = String.Empty;
  53. }
  54. public bool ApplyFormatInEditMode {
  55. get; set;
  56. }
  57. public bool ConvertEmptyStringToNull {
  58. get; set;
  59. }
  60. public virtual string DataField {
  61. get { return ViewState.GetString ("_DataField", String.Empty); }
  62. set {
  63. ViewState ["_DataField"] = value;
  64. OnFieldChanged ();
  65. }
  66. }
  67. public string DataFormatString {
  68. get; set;
  69. }
  70. public override string HeaderText {
  71. get {
  72. string s = ViewState.GetString ("headerText", null);
  73. if (s != null)
  74. return s;
  75. MetaColumn column = MyColumn;
  76. if (column != null)
  77. return column.DisplayName;
  78. return DataField;
  79. }
  80. set { base.HeaderText = value; }
  81. }
  82. public bool HtmlEncode {
  83. get; set;
  84. }
  85. MetaColumn MyColumn {
  86. get {
  87. if (myColumn != null)
  88. return myColumn;
  89. Control owner = Control;
  90. if (owner == null)
  91. return null;
  92. MetaTable table = owner.FindMetaTable ();
  93. if (table == null)
  94. return null;
  95. myColumn = table.GetColumn (DataField);
  96. return myColumn;
  97. }
  98. }
  99. public string NullDisplayText {
  100. get; set;
  101. }
  102. public override string SortExpression {
  103. get {
  104. string s = ViewState.GetString ("sortExpression", null);
  105. if (s != null)
  106. return s;
  107. MetaColumn column = MyColumn;
  108. if (column != null)
  109. return column.SortExpression;
  110. return String.Empty;
  111. }
  112. set { base.SortExpression = value; }
  113. }
  114. public virtual string UIHint {
  115. get {
  116. string s = ViewState.GetString ("uiHint", null);
  117. if (s == null)
  118. return String.Empty;
  119. return s;
  120. }
  121. set {
  122. ViewState ["uiHint"] = value;
  123. OnFieldChanged ();
  124. }
  125. }
  126. [MonoTODO]
  127. protected override void CopyProperties (DataControlField newField)
  128. {
  129. throw new NotImplementedException ();
  130. }
  131. [MonoTODO]
  132. protected override DataControlField CreateField ()
  133. {
  134. throw new NotImplementedException ();
  135. }
  136. [MonoTODO]
  137. public override void ExtractValuesFromCell (IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  138. {
  139. throw new NotImplementedException ();
  140. }
  141. public string GetAttribute (string key)
  142. {
  143. if (attributes == null)
  144. return null;
  145. string ret;
  146. if (attributes.TryGetValue (key, out ret))
  147. return ret;
  148. return null;
  149. }
  150. public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  151. {
  152. if (cellType == DataControlCellType.Header || cellType == DataControlCellType.Footer) {
  153. base.InitializeCell (cell, cellType, rowState, rowIndex);
  154. return;
  155. }
  156. DynamicControl dc = new DynamicControl ();
  157. dc.ApplyFormatInEditMode = ApplyFormatInEditMode;
  158. dc.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  159. dc.Column = MyColumn;
  160. dc.DataField = DataField;
  161. dc.DataFormatString = DataFormatString;
  162. dc.HtmlEncode = HtmlEncode;
  163. dc.Mode = (rowState & DataControlRowState.Edit) != 0 ? DataBoundControlMode.Edit :
  164. (rowState & DataControlRowState.Insert) != 0 ? DataBoundControlMode.Insert : DataBoundControlMode.ReadOnly;
  165. dc.NullDisplayText = NullDisplayText;
  166. dc.UIHint = UIHint;
  167. dc.InternalSetAttributes (attributes);
  168. cell.Controls.Add (dc);
  169. }
  170. public void SetAttribute (string key, string value)
  171. {
  172. if (attributes == null)
  173. attributes = new Dictionary <string, string> ();
  174. if (attributes.ContainsKey (key))
  175. attributes [key] = value;
  176. else
  177. attributes.Add (key, value);
  178. }
  179. }
  180. }