BoundField.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 (true)]
  44. [WebSysDescription ("")]
  45. [WebCategoryAttribute ("Behavior")]
  46. public virtual bool ConvertEmptyStringToNull {
  47. get {
  48. object ob = ViewState ["ConvertEmptyStringToNull"];
  49. if (ob != null) return (bool) ob;
  50. return true;
  51. }
  52. set {
  53. ViewState ["ConvertEmptyStringToNull"] = value;
  54. OnFieldChanged ();
  55. }
  56. }
  57. [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
  58. [WebSysDescription ("")]
  59. [WebCategoryAttribute ("Data")]
  60. [DefaultValueAttribute ("")]
  61. public virtual string DataField {
  62. get {
  63. object ob = ViewState ["DataField"];
  64. if (ob != null) return (string) ob;
  65. return "";
  66. }
  67. set {
  68. ViewState ["DataField"] = value;
  69. OnFieldChanged ();
  70. }
  71. }
  72. [DefaultValueAttribute ("")]
  73. [WebSysDescription ("")]
  74. [WebCategoryAttribute ("Data")]
  75. public virtual string DataFormatString {
  76. get {
  77. object ob = ViewState ["DataFormatString"];
  78. if (ob != null) return (string) ob;
  79. return "";
  80. }
  81. set {
  82. ViewState ["DataFormatString"] = value;
  83. OnFieldChanged ();
  84. }
  85. }
  86. [DefaultValueAttribute ("")]
  87. [WebCategoryAttribute ("Behavior")]
  88. public virtual string NullDisplayText {
  89. get {
  90. object ob = ViewState ["NullDisplayText"];
  91. if (ob != null) return (string) ob;
  92. return "";
  93. }
  94. set {
  95. ViewState ["NullDisplayText"] = value;
  96. OnFieldChanged ();
  97. }
  98. }
  99. [DefaultValueAttribute (false)]
  100. [WebSysDescription ("")]
  101. [WebCategoryAttribute ("Behavior")]
  102. public bool ReadOnly {
  103. get {
  104. object val = ViewState ["ReadOnly"];
  105. return val != null ? (bool) val : false;
  106. }
  107. set {
  108. ViewState ["ReadOnly"] = value;
  109. OnFieldChanged ();
  110. }
  111. }
  112. [DefaultValueAttribute (true)]
  113. [WebSysDescription ("")]
  114. [WebCategoryAttribute ("HtmlEncode")]
  115. public virtual bool HtmlEncode {
  116. get {
  117. object val = ViewState ["HtmlEncode"];
  118. return val != null ? (bool) val : true;
  119. }
  120. set {
  121. ViewState ["HtmlEncode"] = true;
  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. public override void InitializeCell (DataControlFieldCell cell,
  139. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  140. {
  141. base.InitializeCell (cell, cellType, rowState, rowIndex);
  142. if (cellType == DataControlCellType.DataCell) {
  143. InitializeDataCell (cell, rowState);
  144. if ((rowState & DataControlRowState.Insert) == 0)
  145. cell.DataBinding += new EventHandler (OnDataBindField);
  146. }
  147. }
  148. public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  149. {
  150. bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
  151. if (editable && !ReadOnly) {
  152. TextBox box = new TextBox ();
  153. cell.Controls.Add (box);
  154. }
  155. }
  156. protected virtual bool SupportsHtmlEncode {
  157. get { return true; }
  158. }
  159. protected virtual string FormatDataValue (object value, bool encode)
  160. {
  161. string res;
  162. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  163. res = NullDisplayText;
  164. else if (DataFormatString.Length > 0)
  165. res = string.Format (DataFormatString, value);
  166. else
  167. res = value.ToString ();
  168. if (encode) return HttpUtility.HtmlEncode (res);
  169. else return res;
  170. }
  171. protected virtual object GetValue (Control controlContainer)
  172. {
  173. if (DesignMode)
  174. return GetDesignTimeValue ();
  175. else
  176. return GetBoundValue (controlContainer);
  177. }
  178. protected virtual object GetDesignTimeValue ()
  179. {
  180. return GetBoundValue (Control);
  181. }
  182. object GetBoundValue (Control controlContainer)
  183. {
  184. if (DataField == ThisExpression)
  185. return controlContainer.ToString ();
  186. else {
  187. IDataItemContainer dic = (IDataItemContainer) controlContainer;
  188. if (boundProperty == null) {
  189. boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
  190. if (boundProperty == null)
  191. new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
  192. }
  193. return boundProperty.GetValue (dic.DataItem);
  194. }
  195. }
  196. protected virtual void OnDataBindField (object sender, EventArgs e)
  197. {
  198. DataControlFieldCell cell = (DataControlFieldCell) sender;
  199. if (cell.Controls.Count > 0) {
  200. TextBox box = (TextBox) cell.Controls [0];
  201. object val = GetValue (cell.BindingContainer);
  202. box.Text = val != null ? val.ToString() : "";
  203. }
  204. else
  205. cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
  206. }
  207. protected override DataControlField CreateField ()
  208. {
  209. return new BoundField ();
  210. }
  211. protected override void CopyProperties (DataControlField newField)
  212. {
  213. base.CopyProperties (newField);
  214. BoundField field = (BoundField) newField;
  215. field.ConvertEmptyStringToNull = ConvertEmptyStringToNull;
  216. field.DataField = DataField;
  217. field.DataFormatString = DataFormatString;
  218. field.NullDisplayText = NullDisplayText;
  219. field.ReadOnly = ReadOnly;
  220. field.HtmlEncode = HtmlEncode;
  221. }
  222. }
  223. }
  224. #endif