BoundField.cs 9.0 KB

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