TableCell.cs 6.6 KB

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