BoundField.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
  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. }
  106. }
  107. [WebCategoryAttribute ("HtmlEncode")]
  108. [DefaultValueAttribute (true)]
  109. public virtual bool HtmlEncode {
  110. get {
  111. object val = ViewState ["HtmlEncode"];
  112. return val != null ? (bool) val : true;
  113. }
  114. set {
  115. ViewState ["HtmlEncode"] = true;
  116. }
  117. }
  118. public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
  119. DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
  120. {
  121. if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
  122. if (cell.Controls.Count > 0) {
  123. TextBox box = cell.Controls [0] as TextBox;
  124. dictionary [DataField] = box.Text;
  125. }
  126. } else if (includeReadOnly) {
  127. dictionary [DataField] = cell.Text;
  128. }
  129. }
  130. public override void InitializeCell (DataControlFieldCell cell,
  131. DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
  132. {
  133. base.InitializeCell (cell, cellType, rowState, rowIndex);
  134. if (cellType == DataControlCellType.DataCell)
  135. InitializeDataCell (cell, rowState);
  136. cell.DataBinding += new EventHandler (OnDataBindField);
  137. }
  138. public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  139. {
  140. if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) {
  141. TextBox box = new TextBox ();
  142. cell.Controls.Add (box);
  143. }
  144. }
  145. protected virtual bool SupportsHtmlEncode {
  146. get { return true; }
  147. }
  148. protected virtual string FormatDataValue (object value, bool encode)
  149. {
  150. string res;
  151. if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
  152. res = NullDisplayText;
  153. else if (DataFormatString.Length > 0)
  154. res = string.Format (DataFormatString, value);
  155. else
  156. res = value.ToString ();
  157. if (encode) return HttpUtility.HtmlEncode (res);
  158. else return res;
  159. }
  160. protected virtual object GetValue (Control controlContainer)
  161. {
  162. if (DesignMode)
  163. return GetDesignTimeValue ();
  164. else
  165. return GetBoundValue (controlContainer);
  166. }
  167. protected virtual object GetDesignTimeValue ()
  168. {
  169. return GetBoundValue (Control);
  170. }
  171. object GetBoundValue (Control controlContainer)
  172. {
  173. if (DataField == ThisExpression)
  174. return controlContainer.ToString ();
  175. else {
  176. IDataItemContainer dic = controlContainer as IDataItemContainer;
  177. if (boundProperty == null) {
  178. ICustomTypeDescriptor desc = dic.DataItem as ICustomTypeDescriptor;
  179. if (desc != null) {
  180. boundProperty = desc.GetProperties () [DataField];
  181. if (boundProperty != null)
  182. return boundProperty.GetValue (dic.DataItem);
  183. } else {
  184. PropertyInfo pi = dic.DataItem.GetType ().GetProperty (DataField);
  185. if (pi != null)
  186. return pi.GetValue (dic.DataItem, null);
  187. }
  188. throw new InvalidOperationException ("Property '" + DataField + "' not found in data bound item");
  189. }
  190. return boundProperty.GetValue (dic.DataItem);
  191. }
  192. }
  193. protected virtual void OnDataBindField (object sender, EventArgs e)
  194. {
  195. DataControlFieldCell cell = (DataControlFieldCell) sender;
  196. if (cell.Controls.Count > 0) {
  197. TextBox box = cell.Controls [0] as TextBox;
  198. box.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
  199. }
  200. else
  201. cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
  202. }
  203. }
  204. }
  205. #endif