Table.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // System.Web.UI.WebControls.Table.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.Security.Permissions;
  30. namespace System.Web.UI.WebControls {
  31. // CAS
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [DefaultProperty ("Rows")]
  36. [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. [ParseChildren (true, "Rows")]
  38. #if NET_2_0
  39. public class Table : WebControl, IPostBackEventHandler {
  40. #else
  41. public class Table : WebControl {
  42. #endif
  43. private TableRowCollection rows;
  44. public Table ()
  45. : base (HtmlTextWriterTag.Table)
  46. {
  47. }
  48. #if NET_2_0
  49. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  50. [UrlProperty]
  51. #else
  52. [Bindable (true)]
  53. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  54. #endif
  55. [DefaultValue ("")]
  56. [WebSysDescription ("")]
  57. [WebCategory ("Appearance")]
  58. public virtual string BackImageUrl {
  59. get {
  60. if (!ControlStyleCreated)
  61. return String.Empty; // default value
  62. return TableStyle.BackImageUrl;
  63. }
  64. set { TableStyle.BackImageUrl = value; }
  65. }
  66. // note: it seems that Caption and CaptionAlign appeared in 1.1 SP1
  67. [DefaultValue ("")]
  68. #if NET_2_0
  69. [Localizable (true)]
  70. #endif
  71. [WebSysDescription ("")]
  72. [WebCategory ("Accessibility")]
  73. public virtual string Caption {
  74. get {
  75. object o = ViewState ["Caption"];
  76. return (o == null) ? String.Empty : (string) o;
  77. }
  78. set {
  79. if (value == null)
  80. ViewState.Remove ("Caption");
  81. else
  82. ViewState ["Caption"] = value;
  83. }
  84. }
  85. [DefaultValue (TableCaptionAlign.NotSet)]
  86. [WebCategory ("Accessibility")]
  87. public virtual TableCaptionAlign CaptionAlign {
  88. get {
  89. object o = ViewState ["CaptionAlign"];
  90. return (o == null) ? TableCaptionAlign.NotSet : (TableCaptionAlign) o;
  91. }
  92. set {
  93. if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right)) {
  94. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
  95. }
  96. ViewState ["CaptionAlign"] = value;
  97. }
  98. }
  99. #if ONLY_1_1
  100. [Bindable (true)]
  101. #endif
  102. [DefaultValue (-1)]
  103. [WebSysDescription ("")]
  104. [WebCategory ("Appearance")]
  105. public virtual int CellPadding {
  106. get {
  107. if (!ControlStyleCreated)
  108. return -1; // default value
  109. return TableStyle.CellPadding;
  110. }
  111. set { TableStyle.CellPadding = value; }
  112. }
  113. #if ONLY_1_1
  114. [Bindable (true)]
  115. #endif
  116. [DefaultValue (-1)]
  117. [WebSysDescription ("")]
  118. [WebCategory ("Appearance")]
  119. public virtual int CellSpacing {
  120. get {
  121. if (!ControlStyleCreated)
  122. return -1; // default value
  123. return TableStyle.CellSpacing;
  124. }
  125. set { TableStyle.CellSpacing = value; }
  126. }
  127. #if ONLY_1_1
  128. [Bindable (true)]
  129. #endif
  130. [DefaultValue (GridLines.None)]
  131. [WebSysDescription ("")]
  132. [WebCategory ("Appearance")]
  133. public virtual GridLines GridLines {
  134. get {
  135. if (!ControlStyleCreated)
  136. return GridLines.None; // default value
  137. return TableStyle.GridLines;
  138. }
  139. set { TableStyle.GridLines = value; }
  140. }
  141. #if ONLY_1_1
  142. [Bindable (true)]
  143. #endif
  144. [DefaultValue (HorizontalAlign.NotSet)]
  145. [WebSysDescription ("")]
  146. [WebCategory ("Layout")]
  147. public virtual HorizontalAlign HorizontalAlign {
  148. get {
  149. if (!ControlStyleCreated)
  150. return HorizontalAlign.NotSet; // default value
  151. return TableStyle.HorizontalAlign;
  152. }
  153. set { TableStyle.HorizontalAlign = value; }
  154. }
  155. [MergableProperty (false)]
  156. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  157. [WebSysDescription ("")]
  158. public virtual TableRowCollection Rows {
  159. get {
  160. if (rows == null)
  161. rows = new TableRowCollection (this);
  162. return rows;
  163. }
  164. }
  165. private TableStyle TableStyle {
  166. get { return (ControlStyle as TableStyle); }
  167. }
  168. protected override void AddAttributesToRender (HtmlTextWriter writer)
  169. {
  170. base.AddAttributesToRender (writer);
  171. if (!ControlStyleCreated || TableStyle.IsEmpty) {
  172. // for some reason border=X seems to be always present
  173. // and isn't rendered as a style attribute
  174. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  175. }
  176. }
  177. protected override ControlCollection CreateControlCollection ()
  178. {
  179. return new RowControlCollection (this);
  180. }
  181. protected override Style CreateControlStyle ()
  182. {
  183. return new TableStyle (ViewState);
  184. }
  185. #if NET_2_0
  186. protected internal
  187. #else
  188. protected
  189. #endif
  190. override void RenderContents (HtmlTextWriter writer)
  191. {
  192. if (HasControls ()) {
  193. writer.Indent++;
  194. foreach (Control c in Controls) {
  195. c.RenderControl (writer);
  196. }
  197. writer.Indent--;
  198. }
  199. }
  200. // new in Fx 1.1 SP1 (to support Caption and CaptionAlign)
  201. public override void RenderBeginTag (HtmlTextWriter writer)
  202. {
  203. base.RenderBeginTag (writer);
  204. string s = Caption;
  205. if (s.Length > 0) {
  206. writer.WriteBeginTag ("caption");
  207. TableCaptionAlign tca = CaptionAlign;
  208. if (tca != TableCaptionAlign.NotSet) {
  209. writer.WriteAttribute ("align", tca.ToString ());
  210. }
  211. writer.Write (HtmlTextWriter.TagRightChar);
  212. writer.Indent++;
  213. writer.WriteLine ();
  214. writer.Write (s);
  215. writer.Indent--;
  216. writer.WriteLine ();
  217. writer.WriteEndTag ("caption");
  218. } else if (HasControls ()) {
  219. writer.Indent++;
  220. }
  221. }
  222. #if NET_2_0
  223. void IPostBackEventHandler.RaisePostBackEvent (string argument)
  224. {
  225. RaisePostBackEvent (argument);
  226. }
  227. [MonoTODO]
  228. protected virtual void RaisePostBackEvent (string argument)
  229. {
  230. }
  231. #endif
  232. // inner class
  233. protected class RowControlCollection : ControlCollection {
  234. internal RowControlCollection (Table owner)
  235. : base (owner)
  236. {
  237. }
  238. public override void Add (Control child)
  239. {
  240. if (child == null)
  241. throw new NullReferenceException ("null");
  242. if (!(child is TableRow))
  243. throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
  244. base.Add (child);
  245. }
  246. public override void AddAt (int index, Control child)
  247. {
  248. if (child == null)
  249. throw new NullReferenceException ("null");
  250. if (!(child is TableRow))
  251. throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
  252. base.AddAt (index, child);
  253. }
  254. }
  255. }
  256. }