BoundField.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // System.Web.UI.WebControls.BoundField.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  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.Web.UI;
  33. using System.ComponentModel;
  34. using System.Security.Permissions;
  35. using System.Reflection;
  36. namespace System.Web.UI.WebControls {
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public class BoundField : DataControlField
  40. {
  41. public static readonly string ThisExpression = "!";
  42. PropertyDescriptor boundProperty;
  43. [MonoTODO]
  44. [DefaultValueAttribute (false)]
  45. [WebSysDescription ("")]
  46. [WebCategoryAttribute ("Behavior")]
  47. public virtual bool ApplyFormatInEditMode {
  48. get {
  49. throw new NotImplementedException ();
  50. }
  51. set {
  52. throw new NotImplementedException ();
  53. }
  54. }
  55. [DefaultValueAttribute (true)]
  56. [WebSysDescription ("")]
  57. [WebCategoryAttribute ("Behavior")]
  58. public virtual bool ConvertEmptyStringToNull {
  59. get { return ViewState.GetBool ("ConvertEmptyStringToNull", false); }
  60. set {
  61. ViewState ["ConvertEmptyStringToNull"] = value;
  62. OnFieldChanged ();
  63. }
  64. }
  65. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  66. [WebSysDescription ("")]
  67. [WebCategoryAttribute ("Data")]
  68. [DefaultValueAttribute ("")]
  69. public virtual string DataField {
  70. get { return ViewState.GetString ("DataField", ""); }
  71. set {
  72. ViewState ["DataField"] = value;
  73. OnFieldChanged ();
  74. }
  75. }
  76. [DefaultValueAttribute ("")]
  77. [WebSysDescription ("")]
  78. [WebCategoryAttribute ("Data")]
  79. public virtual string DataFormatString {
  80. get { return ViewState.GetString ("DataFormatString", ""); }
  81. set {
  82. ViewState ["DataFormatString"] = value;
  83. OnFieldChanged ();
  84. }
  85. }
  86. [MonoTODO]
  87. [WebSysDescription ("")]
  88. [WebCategoryAttribute ("Appearance")]
  89. public override string HeaderText {
  90. get { return ViewState.GetString ("HeaderText", "");
  91. }
  92. set {
  93. ViewState["HeaderText"] = value;
  94. OnFieldChanged ();
  95. }
  96. }
  97. [DefaultValueAttribute ("")]
  98. [WebCategoryAttribute ("Behavior")]
  99. public virtual string NullDisplayText {
  100. get { return ViewState.GetString ("NullDisplaytext", ""); }
  101. set {
  102. ViewState ["NullDisplayText"] = value;
  103. OnFieldChanged ();
  104. }
  105. }
  106. [DefaultValueAttribute (false)]
  107. [WebSysDescription ("")]
  108. [WebCategoryAttribute ("Behavior")]
  109. public virtual bool ReadOnly {
  110. get { return ViewState.GetBool ("ReadOnly", false); }
  111. set {
  112. ViewState ["ReadOnly"] = value;
  113. OnFieldChanged ();
  114. }
  115. }
  116. [DefaultValueAttribute (true)]
  117. [WebSysDescription ("")]
  118. [WebCategoryAttribute ("HtmlEncode")]
  119. public virtual bool HtmlEncode {
  120. get { return ViewState.GetBool ("HtmlEncode", true); }
  121. set {
  122. ViewState ["HtmlEncode"] = value;
  123. OnFieldChanged ();
  124. }
  125. }
  126. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  127. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  128. {
  129. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  130. if (editable && !ReadOnly) {
  131. if (cell.Controls.Count > 0) {
  132. TextBox box = (TextBox) cell.Controls [0];
  133. dictionary [DataField] = box.Text;
  134. }
  135. } else if (includeReadOnly) {
  136. dictionary [DataField] = cell.Text;
  137. }
  138. }
  139. [MonoTODO]
  140. public override bool Initialize (bool enableSorting,
  141. Control control)
  142. {
  143. return base.Initialize (enableSorting, control);
  144. }
  145. public override void InitializeCell (DataControlFieldCell cell,
  146. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  147. {
  148. base.InitializeCell (cell, cellType, rowState, rowIndex);
  149. if (cellType == DataControlCellType.DataCell) {
  150. InitializeDataCell (cell, rowState);
  151. if ((rowState & DataControlRowState.Insert) == 0)
  152. cell.DataBinding += new EventHandler (OnDataBindField);
  153. }
  154. }
  155. protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  156. {
  157. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  158. if (editable && !ReadOnly) {
  159. TextBox box = new TextBox ();
  160. box.ID = cell.ClientID;
  161. cell.Controls.Add (box);
  162. }
  163. }
  164. protected virtual bool SupportsHtmlEncode {
  165. get { return true; }
  166. }
  167. protected virtual string FormatDataValue (object value, bool encode)
  168. {
  169. string res;
  170. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  171. res = NullDisplayText;
  172. else if (DataFormatString.Length > 0)
  173. res = string.Format (DataFormatString, value);
  174. else
  175. res = value.ToString ();
  176. if (encode) return HttpUtility.HtmlEncode (res);
  177. else return res;
  178. }
  179. protected virtual object GetValue (Control controlContainer)
  180. {
  181. if (DesignMode)
  182. return GetDesignTimeValue ();
  183. else
  184. return GetBoundValue (controlContainer);
  185. }
  186. protected virtual object GetDesignTimeValue ()
  187. {
  188. return GetBoundValue (Control);
  189. }
  190. object GetBoundValue (Control controlContainer)
  191. {
  192. if (DataField == ThisExpression)
  193. return controlContainer.ToString ();
  194. else {
  195. IDataItemContainer dic = (IDataItemContainer) controlContainer;
  196. if (boundProperty == null) {
  197. boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
  198. if (boundProperty == null)
  199. throw new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
  200. }
  201. return boundProperty.GetValue (dic.DataItem);
  202. }
  203. }
  204. protected virtual void OnDataBindField (object sender, EventArgs e)
  205. {
  206. DataControlFieldCell cell = (DataControlFieldCell) sender;
  207. if (cell.Controls.Count > 0) {
  208. TextBox box = (TextBox) cell.Controls [0];
  209. object val = GetValue (cell.BindingContainer);
  210. box.Text = val != null ? val.ToString() : "";
  211. }
  212. else
  213. cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
  214. }
  215. protected override DataControlField CreateField ()
  216. {
  217. return new BoundField ();
  218. }
  219. protected override void CopyProperties (DataControlField newField)
  220. {
  221. base.CopyProperties (newField);
  222. BoundField field = (BoundField) newField;
  223. field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  224. field.DataField = DataField;
  225. field.DataFormatString = DataFormatString;
  226. field.NullDisplayText = NullDisplayText;
  227. field.ReadOnly = ReadOnly;
  228. field.HtmlEncode = HtmlEncode;
  229. }
  230. [MonoTODO]
  231. public override void ValidateSupportsCallback ()
  232. {
  233. throw new NotImplementedException ();
  234. }
  235. }
  236. }
  237. #endif