BoundField.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. [DefaultValueAttribute (false)]
  44. [WebSysDescription ("")]
  45. [WebCategoryAttribute ("Behavior")]
  46. public virtual bool ApplyFormatInEditMode {
  47. get {
  48. return ViewState.GetBool ("ApplyFormatInEditMode", false);
  49. }
  50. set {
  51. ViewState ["ApplyFormatInEditMode"] = value;
  52. }
  53. }
  54. [DefaultValueAttribute (true)]
  55. [WebSysDescription ("")]
  56. [WebCategoryAttribute ("Behavior")]
  57. public virtual bool ConvertEmptyStringToNull {
  58. get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
  59. set {
  60. ViewState ["ConvertEmptyStringToNull"] = value;
  61. OnFieldChanged ();
  62. }
  63. }
  64. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  65. [WebSysDescription ("")]
  66. [WebCategoryAttribute ("Data")]
  67. [DefaultValueAttribute ("")]
  68. public virtual string DataField {
  69. get { return ViewState.GetString ("DataField", ""); }
  70. set {
  71. ViewState ["DataField"] = value;
  72. OnFieldChanged ();
  73. }
  74. }
  75. [DefaultValueAttribute ("")]
  76. [WebSysDescription ("")]
  77. [WebCategoryAttribute ("Data")]
  78. public virtual string DataFormatString {
  79. get { return ViewState.GetString ("DataFormatString", ""); }
  80. set {
  81. ViewState ["DataFormatString"] = value;
  82. OnFieldChanged ();
  83. }
  84. }
  85. [MonoTODO]
  86. [WebSysDescription ("")]
  87. [WebCategoryAttribute ("Appearance")]
  88. public override string HeaderText {
  89. get { return ViewState.GetString ("HeaderText", "");
  90. }
  91. set {
  92. ViewState["HeaderText"] = value;
  93. OnFieldChanged ();
  94. }
  95. }
  96. [DefaultValueAttribute ("")]
  97. [WebCategoryAttribute ("Behavior")]
  98. public virtual string NullDisplayText {
  99. get { return ViewState.GetString ("NullDisplayText", ""); }
  100. set {
  101. ViewState ["NullDisplayText"] = value;
  102. OnFieldChanged ();
  103. }
  104. }
  105. [DefaultValueAttribute (false)]
  106. [WebSysDescription ("")]
  107. [WebCategoryAttribute ("Behavior")]
  108. public virtual bool ReadOnly {
  109. get { return ViewState.GetBool ("ReadOnly", false); }
  110. set {
  111. ViewState ["ReadOnly"] = value;
  112. OnFieldChanged ();
  113. }
  114. }
  115. [DefaultValueAttribute (true)]
  116. [WebSysDescription ("")]
  117. [WebCategoryAttribute ("HtmlEncode")]
  118. public virtual bool HtmlEncode {
  119. get { return ViewState.GetBool ("HtmlEncode", true); }
  120. set {
  121. ViewState ["HtmlEncode"] = value;
  122. OnFieldChanged ();
  123. }
  124. }
  125. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  126. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  127. {
  128. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  129. if (editable && !ReadOnly) {
  130. if (cell.Controls.Count > 0) {
  131. TextBox box = (TextBox) cell.Controls [0];
  132. dictionary [DataField] = box.Text;
  133. }
  134. } else if (includeReadOnly) {
  135. dictionary [DataField] = cell.Text;
  136. }
  137. }
  138. [MonoTODO]
  139. public override bool Initialize (bool enableSorting,
  140. Control control)
  141. {
  142. return base.Initialize (enableSorting, control);
  143. }
  144. public override void InitializeCell (DataControlFieldCell cell,
  145. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  146. {
  147. base.InitializeCell (cell, cellType, rowState, rowIndex);
  148. if (cellType == DataControlCellType.DataCell) {
  149. InitializeDataCell (cell, rowState);
  150. if ((rowState & DataControlRowState.Insert) == 0)
  151. cell.DataBinding += new EventHandler (OnDataBindField);
  152. }
  153. }
  154. protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  155. {
  156. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  157. if (editable && !ReadOnly) {
  158. TextBox box = new TextBox ();
  159. box.ID = cell.ClientID;
  160. cell.Controls.Add (box);
  161. }
  162. }
  163. protected virtual bool SupportsHtmlEncode {
  164. get { return true; }
  165. }
  166. protected virtual string FormatDataValue (object value, bool encode)
  167. {
  168. string res;
  169. if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
  170. if (NullDisplayText.Length == 0) {
  171. encode = false;
  172. res = " ";
  173. }
  174. else
  175. res = NullDisplayText;
  176. }
  177. else if (DataFormatString.Length > 0)
  178. res = string.Format (DataFormatString, value);
  179. else
  180. res = value.ToString ();
  181. if (encode) return HttpUtility.HtmlEncode (res);
  182. else return res;
  183. }
  184. protected virtual object GetValue (Control controlContainer)
  185. {
  186. if (DesignMode)
  187. return GetDesignTimeValue ();
  188. else
  189. return GetBoundValue (controlContainer);
  190. }
  191. protected virtual object GetDesignTimeValue ()
  192. {
  193. return "Databound";
  194. }
  195. object GetBoundValue (Control controlContainer)
  196. {
  197. object dataItem = DataBinder.GetDataItem (controlContainer);
  198. if (dataItem == null)
  199. throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
  200. if (DataField == ThisExpression)
  201. return dataItem.ToString ();
  202. else if (DataField == string.Empty)
  203. return null;
  204. return DataBinder.GetPropertyValue (dataItem, DataField);
  205. }
  206. protected virtual void OnDataBindField (object sender, EventArgs e)
  207. {
  208. Control cell = (Control) sender;
  209. Control controlContainer = cell.BindingContainer;
  210. if (!(controlContainer is INamingContainer))
  211. throw new HttpException ("A DataControlField must be within an INamingContainer.");
  212. object val = GetValue (controlContainer);
  213. if (cell.Controls.Count > 0) {
  214. TextBox box = (TextBox) cell.Controls [0];
  215. if (ApplyFormatInEditMode)
  216. box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
  217. else
  218. box.Text = val != null ? val.ToString() : NullDisplayText;
  219. }
  220. else
  221. ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
  222. }
  223. protected override DataControlField CreateField ()
  224. {
  225. return new BoundField ();
  226. }
  227. protected override void CopyProperties (DataControlField newField)
  228. {
  229. base.CopyProperties (newField);
  230. BoundField field = (BoundField) newField;
  231. field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  232. field.DataField = DataField;
  233. field.DataFormatString = DataFormatString;
  234. field.NullDisplayText = NullDisplayText;
  235. field.ReadOnly = ReadOnly;
  236. field.HtmlEncode = HtmlEncode;
  237. }
  238. // MSDN: The ValidateSupportsCallback method is a helper method used to determine
  239. // whether the controls contained in a BoundField object support callbacks.
  240. // This method has been implemented as an empty method (a method that does not contain
  241. // any code) to indicate that callbacks are supported.
  242. public override void ValidateSupportsCallback ()
  243. {
  244. }
  245. }
  246. }
  247. #endif