TableCell.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // System.Web.UI.WebControls.TableCell.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.Globalization;
  30. using System.Security.Permissions;
  31. using System.Text;
  32. using System.Web.Util;
  33. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [ControlBuilder (typeof (TableCellControlBuilder))]
  39. [DefaultProperty ("Text")]
  40. [ParseChildren (false)]
  41. [ToolboxItem ("")]
  42. #if NET_2_0
  43. [Bindable (false)]
  44. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  45. #else
  46. [PersistChildren (true)]
  47. #endif
  48. public class TableCell : WebControl {
  49. public TableCell ()
  50. : base (HtmlTextWriterTag.Td)
  51. {
  52. AutoID = false;
  53. }
  54. // FIXME: is there a clean way to change the tag's name without using a ctor ?
  55. // if not then this truly limits the usefulness of inheritance
  56. internal TableCell (HtmlTextWriterTag tag)
  57. : base (tag)
  58. {
  59. AutoID = false;
  60. }
  61. #if NET_2_0
  62. [DefaultValue (null)]
  63. [TypeConverter (typeof (StringArrayConverter))]
  64. public virtual string[] AssociatedHeaderCellID {
  65. get {
  66. object o = ViewState ["AssociatedHeaderCellID"];
  67. return (o == null) ? new string[0] : (string[]) o;
  68. }
  69. set {
  70. if (value == null)
  71. ViewState.Remove ("AssociatedHeaderCellID");
  72. else
  73. ViewState ["AssociatedHeaderCellID"] = value;
  74. }
  75. }
  76. #endif
  77. #if ONLY_1_1
  78. [Bindable (true)]
  79. #endif
  80. [DefaultValue (0)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Appearance")]
  83. public virtual int ColumnSpan {
  84. get {
  85. object o = ViewState ["ColumnSpan"];
  86. return (o == null) ? 0 : (int) o;
  87. }
  88. set {
  89. // LAMESPEC: undocumented (but like Table.CellPadding)
  90. if (value < 0)
  91. throw new ArgumentOutOfRangeException ("< 0");
  92. ViewState ["ColumnSpan"] = value;
  93. }
  94. }
  95. #if ONLY_1_1
  96. [Bindable (true)]
  97. #endif
  98. [DefaultValue (HorizontalAlign.NotSet)]
  99. [WebSysDescription ("")]
  100. [WebCategory ("Layout")]
  101. public virtual HorizontalAlign HorizontalAlign {
  102. get {
  103. if (!ControlStyleCreated)
  104. return HorizontalAlign.NotSet; // default value
  105. return TableItemStyle.HorizontalAlign;
  106. }
  107. set { TableItemStyle.HorizontalAlign = value; }
  108. }
  109. #if ONLY_1_1
  110. [Bindable (true)]
  111. #endif
  112. [DefaultValue (0)]
  113. [WebSysDescription ("")]
  114. [WebCategory ("Layout")]
  115. public virtual int RowSpan {
  116. get {
  117. object o = ViewState ["RowSpan"];
  118. return (o == null) ? 0 : (int) o;
  119. }
  120. set {
  121. // LAMESPEC: undocumented (but like Table.CellPadding)
  122. if (value < 0)
  123. throw new ArgumentOutOfRangeException ("< 0");
  124. ViewState ["RowSpan"] = value;
  125. }
  126. }
  127. #if NET_2_0
  128. [Localizable (true)]
  129. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  130. #else
  131. [Bindable (true)]
  132. #endif
  133. [DefaultValue ("")]
  134. [WebSysDescription ("")]
  135. [WebCategory ("Appearance")]
  136. public virtual string Text {
  137. get {
  138. object o = ViewState ["Text"];
  139. return (o == null) ? String.Empty : (string) o;
  140. }
  141. set {
  142. if (value == null)
  143. ViewState.Remove ("Text");
  144. else {
  145. ViewState ["Text"] = value;
  146. if (HasControls ())
  147. Controls.Clear ();
  148. }
  149. }
  150. }
  151. #if ONLY_1_1
  152. [Bindable (true)]
  153. #endif
  154. [DefaultValue (VerticalAlign.NotSet)]
  155. [WebSysDescription ("")]
  156. [WebCategory ("Layout")]
  157. public virtual VerticalAlign VerticalAlign {
  158. get {
  159. if (!ControlStyleCreated)
  160. return VerticalAlign.NotSet; // default value
  161. return TableItemStyle.VerticalAlign;
  162. }
  163. set { TableItemStyle.VerticalAlign = value; }
  164. }
  165. #if ONLY_1_1
  166. [Bindable (true)]
  167. #endif
  168. [DefaultValue (true)]
  169. [WebSysDescription ("")]
  170. [WebCategory ("Layout")]
  171. public virtual bool Wrap {
  172. get {
  173. if (!ControlStyleCreated)
  174. return true; // default value
  175. return TableItemStyle.Wrap;
  176. }
  177. set { TableItemStyle.Wrap = value; }
  178. }
  179. #if NET_4_0
  180. public override bool SupportsDisabledAttribute {
  181. get { return RenderingCompatibilityLessThan40; }
  182. }
  183. #endif
  184. TableItemStyle TableItemStyle {
  185. get { return (ControlStyle as TableItemStyle); }
  186. }
  187. protected override void AddAttributesToRender (HtmlTextWriter writer)
  188. {
  189. base.AddAttributesToRender (writer);
  190. if (writer == null)
  191. return;
  192. int i = ColumnSpan;
  193. if (i > 0)
  194. writer.AddAttribute (HtmlTextWriterAttribute.Colspan, i.ToString (Helpers.InvariantCulture), false);
  195. i = RowSpan;
  196. if (i > 0)
  197. writer.AddAttribute (HtmlTextWriterAttribute.Rowspan, i.ToString (Helpers.InvariantCulture), false);
  198. #if NET_2_0
  199. string[] ahci = AssociatedHeaderCellID;
  200. if (ahci.Length > 1) {
  201. StringBuilder sb = new StringBuilder ();
  202. for (i = 0; i < ahci.Length - 1; i++) {
  203. sb.Append (ahci [i]);
  204. sb.Append (",");
  205. }
  206. sb.Append (ahci.Length - 1);
  207. writer.AddAttribute (HtmlTextWriterAttribute.Headers, sb.ToString ());
  208. } else if (ahci.Length == 1) {
  209. // most common case (without a StringBuilder)
  210. writer.AddAttribute (HtmlTextWriterAttribute.Headers, ahci [0]);
  211. }
  212. #endif
  213. }
  214. protected override void AddParsedSubObject (object obj)
  215. {
  216. if (HasControls ()) {
  217. base.AddParsedSubObject (obj);
  218. return;
  219. }
  220. LiteralControl lc = (obj as LiteralControl);
  221. if (lc == null) {
  222. string s = Text;
  223. if (s.Length > 0) {
  224. Controls.Add (new LiteralControl (s));
  225. // remove from viewstate
  226. Text = null;
  227. }
  228. base.AddParsedSubObject(obj);
  229. } else {
  230. // this will clear any existing controls
  231. Text = lc.Text;
  232. }
  233. }
  234. protected override Style CreateControlStyle ()
  235. {
  236. return new TableItemStyle (ViewState);
  237. }
  238. #if NET_2_0
  239. protected internal
  240. #else
  241. protected
  242. #endif
  243. override void RenderContents (HtmlTextWriter writer)
  244. {
  245. if (HasControls () || HasRenderMethodDelegate ())
  246. base.RenderContents (writer);
  247. else
  248. writer.Write (Text);
  249. }
  250. }
  251. }