BoundField.cs 7.1 KB

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