TableCell.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. namespace System.Web.UI.WebControls {
  33. // CAS
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [ControlBuilder (typeof (TableCellControlBuilder))]
  38. [DefaultProperty ("Text")]
  39. [ParseChildren (false)]
  40. [ToolboxItem ("")]
  41. #if NET_2_0
  42. [Bindable (false)]
  43. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  44. #else
  45. [PersistChildren (true)]
  46. #endif
  47. public class TableCell : WebControl {
  48. public TableCell ()
  49. : base (HtmlTextWriterTag.Td)
  50. {
  51. }
  52. // FIXME: is there a clean way to change the tag's name without using a ctor ?
  53. // if not then this truly limits the usefulness of inheritance
  54. internal TableCell (HtmlTextWriterTag tag)
  55. : base (tag)
  56. {
  57. }
  58. #if NET_2_0
  59. [DefaultValue (null)]
  60. [TypeConverter (typeof (StringArrayConverter))]
  61. public virtual string[] AssociatedHeaderCellID {
  62. get {
  63. object o = ViewState ["AssociatedHeaderCellID"];
  64. return (o == null) ? new string[0] : (string[]) o;
  65. }
  66. set {
  67. if (value == null)
  68. ViewState.Remove ("AssociatedHeaderCellID");
  69. else
  70. ViewState ["AssociatedHeaderCellID"] = value;
  71. }
  72. }
  73. #endif
  74. #if ONLY_1_1
  75. [Bindable (true)]
  76. #endif
  77. [DefaultValue (0)]
  78. [WebSysDescription ("")]
  79. [WebCategory ("Appearance")]
  80. public virtual int ColumnSpan {
  81. get {
  82. object o = ViewState ["ColumnSpan"];
  83. return (o == null) ? 0 : (int) o;
  84. }
  85. set {
  86. // LAMESPEC: undocumented (but like Table.CellPadding)
  87. if (value < 0)
  88. throw new ArgumentOutOfRangeException ("< 0");
  89. ViewState ["ColumnSpan"] = value;
  90. }
  91. }
  92. #if ONLY_1_1
  93. [Bindable (true)]
  94. #endif
  95. [DefaultValue (HorizontalAlign.NotSet)]
  96. [WebSysDescription ("")]
  97. [WebCategory ("Layout")]
  98. public virtual HorizontalAlign HorizontalAlign {
  99. get {
  100. if (!ControlStyleCreated)
  101. return HorizontalAlign.NotSet; // default value
  102. return TableItemStyle.HorizontalAlign;
  103. }
  104. set { TableItemStyle.HorizontalAlign = value; }
  105. }
  106. #if ONLY_1_1
  107. [Bindable (true)]
  108. #endif
  109. [DefaultValue (0)]
  110. [WebSysDescription ("")]
  111. [WebCategory ("Layout")]
  112. public virtual int RowSpan {
  113. get {
  114. object o = ViewState ["RowSpan"];
  115. return (o == null) ? 0 : (int) o;
  116. }
  117. set {
  118. // LAMESPEC: undocumented (but like Table.CellPadding)
  119. if (value < 0)
  120. throw new ArgumentOutOfRangeException ("< 0");
  121. ViewState ["RowSpan"] = value;
  122. }
  123. }
  124. #if NET_2_0
  125. [Localizable (true)]
  126. [PersistenceMode (PersistenceMode.EncodedInnerDefaultProperty)]
  127. #else
  128. [Bindable (true)]
  129. #endif
  130. [DefaultValue ("")]
  131. [WebSysDescription ("")]
  132. [WebCategory ("Appearance")]
  133. public virtual string Text {
  134. get {
  135. object o = ViewState ["Text"];
  136. return (o == null) ? String.Empty : (string) o;
  137. }
  138. set {
  139. if (value == null)
  140. ViewState.Remove ("Text");
  141. else {
  142. ViewState ["Text"] = value;
  143. if (HasControls ())
  144. Controls.Clear ();
  145. }
  146. }
  147. }
  148. #if ONLY_1_1
  149. [Bindable (true)]
  150. #endif
  151. [DefaultValue (VerticalAlign.NotSet)]
  152. [WebSysDescription ("")]
  153. [WebCategory ("Layout")]
  154. public virtual VerticalAlign VerticalAlign {
  155. get {
  156. if (!ControlStyleCreated)
  157. return VerticalAlign.NotSet; // default value
  158. return TableItemStyle.VerticalAlign;
  159. }
  160. set { TableItemStyle.VerticalAlign = value; }
  161. }
  162. #if ONLY_1_1
  163. [Bindable (true)]
  164. #endif
  165. [DefaultValue (true)]
  166. [WebSysDescription ("")]
  167. [WebCategory ("Layout")]
  168. public virtual bool Wrap {
  169. get {
  170. if (!ControlStyleCreated)
  171. return true; // default value
  172. return TableItemStyle.Wrap;
  173. }
  174. set { TableItemStyle.Wrap = value; }
  175. }
  176. private TableItemStyle TableItemStyle {
  177. get { return (ControlStyle as TableItemStyle); }
  178. }
  179. protected override void AddAttributesToRender (HtmlTextWriter writer)
  180. {
  181. base.AddAttributesToRender (writer);
  182. if (writer == null)
  183. return;
  184. int i = ColumnSpan;
  185. if (i > 0)
  186. writer.AddAttribute (HtmlTextWriterAttribute.Colspan, i.ToString (CultureInfo.InvariantCulture));
  187. i = RowSpan;
  188. if (i > 0)
  189. writer.AddAttribute (HtmlTextWriterAttribute.Rowspan, i.ToString (CultureInfo.InvariantCulture));
  190. #if NET_2_0
  191. string[] ahci = AssociatedHeaderCellID;
  192. if (ahci.Length > 1) {
  193. StringBuilder sb = new StringBuilder ();
  194. for (i = 0; i < ahci.Length - 1; i++) {
  195. sb.Append (ahci [i]);
  196. sb.Append (",");
  197. }
  198. sb.Append (ahci.Length - 1);
  199. writer.AddAttribute (HtmlTextWriterAttribute.Headers, sb.ToString ());
  200. } else if (ahci.Length == 1) {
  201. // most common case (without a StringBuilder)
  202. writer.AddAttribute (HtmlTextWriterAttribute.Headers, ahci [0]);
  203. }
  204. #endif
  205. }
  206. protected override void AddParsedSubObject (object obj)
  207. {
  208. LiteralControl lc = (obj as LiteralControl);
  209. if (lc == null) {
  210. string s = Text;
  211. if (s.Length > 0) {
  212. Controls.Add (new LiteralControl (s));
  213. // remove from viewstate
  214. Text = null;
  215. }
  216. base.AddParsedSubObject(obj);
  217. } else {
  218. // this will clear any existing controls
  219. Text = lc.Text;
  220. }
  221. }
  222. protected override Style CreateControlStyle ()
  223. {
  224. return new TableItemStyle (ViewState);
  225. }
  226. #if NET_2_0
  227. protected internal
  228. #else
  229. protected
  230. #endif
  231. override void RenderContents (HtmlTextWriter writer)
  232. {
  233. object o = ViewState ["Text"];
  234. if (o != null) {
  235. writer.Write (o as string);
  236. }
  237. base.RenderContents (writer);
  238. }
  239. }
  240. }