2
0

Table.cs 8.1 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. [SupportsEventValidation]
  39. public class Table : WebControl, IPostBackEventHandler
  40. {
  41. TableRowCollection rows;
  42. bool generateTableSections;
  43. public Table ()
  44. : base (HtmlTextWriterTag.Table)
  45. {
  46. }
  47. internal bool GenerateTableSections {
  48. get { return generateTableSections; }
  49. set { generateTableSections = value; }
  50. }
  51. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  52. [UrlProperty]
  53. [DefaultValue ("")]
  54. [WebSysDescription ("")]
  55. [WebCategory ("Appearance")]
  56. public virtual string BackImageUrl {
  57. get {
  58. if (!ControlStyleCreated)
  59. return String.Empty; // default value
  60. return TableStyle.BackImageUrl;
  61. }
  62. set { TableStyle.BackImageUrl = value; }
  63. }
  64. // note: it seems that Caption and CaptionAlign appeared in 1.1 SP1
  65. [DefaultValue ("")]
  66. [Localizable (true)]
  67. [WebSysDescription ("")]
  68. [WebCategory ("Accessibility")]
  69. public virtual string Caption {
  70. get {
  71. object o = ViewState ["Caption"];
  72. return (o == null) ? String.Empty : (string) o;
  73. }
  74. set {
  75. if (value == null)
  76. ViewState.Remove ("Caption");
  77. else
  78. ViewState ["Caption"] = value;
  79. }
  80. }
  81. [DefaultValue (TableCaptionAlign.NotSet)]
  82. [WebCategory ("Accessibility")]
  83. public virtual TableCaptionAlign CaptionAlign {
  84. get {
  85. object o = ViewState ["CaptionAlign"];
  86. return (o == null) ? TableCaptionAlign.NotSet : (TableCaptionAlign) o;
  87. }
  88. set {
  89. if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right)) {
  90. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
  91. }
  92. ViewState ["CaptionAlign"] = value;
  93. }
  94. }
  95. [DefaultValue (-1)]
  96. [WebSysDescription ("")]
  97. [WebCategory ("Appearance")]
  98. public virtual int CellPadding {
  99. get {
  100. if (!ControlStyleCreated)
  101. return -1; // default value
  102. return TableStyle.CellPadding;
  103. }
  104. set { TableStyle.CellPadding = value; }
  105. }
  106. [DefaultValue (-1)]
  107. [WebSysDescription ("")]
  108. [WebCategory ("Appearance")]
  109. public virtual int CellSpacing {
  110. get {
  111. if (!ControlStyleCreated)
  112. return -1; // default value
  113. return TableStyle.CellSpacing;
  114. }
  115. set { TableStyle.CellSpacing = value; }
  116. }
  117. [DefaultValue (GridLines.None)]
  118. [WebSysDescription ("")]
  119. [WebCategory ("Appearance")]
  120. public virtual GridLines GridLines {
  121. get {
  122. if (!ControlStyleCreated)
  123. return GridLines.None; // default value
  124. return TableStyle.GridLines;
  125. }
  126. set { TableStyle.GridLines = value; }
  127. }
  128. [DefaultValue (HorizontalAlign.NotSet)]
  129. [WebSysDescription ("")]
  130. [WebCategory ("Layout")]
  131. public virtual HorizontalAlign HorizontalAlign {
  132. get {
  133. if (!ControlStyleCreated)
  134. return HorizontalAlign.NotSet; // default value
  135. return TableStyle.HorizontalAlign;
  136. }
  137. set { TableStyle.HorizontalAlign = value; }
  138. }
  139. [MergableProperty (false)]
  140. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  141. [WebSysDescription ("")]
  142. public virtual TableRowCollection Rows {
  143. get {
  144. if (rows == null)
  145. rows = new TableRowCollection (this);
  146. return rows;
  147. }
  148. }
  149. private TableStyle TableStyle {
  150. get { return (ControlStyle as TableStyle); }
  151. }
  152. public override bool SupportsDisabledAttribute {
  153. get { return RenderingCompatibilityLessThan40; }
  154. }
  155. protected override void AddAttributesToRender (HtmlTextWriter writer)
  156. {
  157. base.AddAttributesToRender (writer);
  158. }
  159. protected override ControlCollection CreateControlCollection ()
  160. {
  161. return new RowControlCollection (this);
  162. }
  163. protected override Style CreateControlStyle ()
  164. {
  165. return new TableStyle (ViewState);
  166. }
  167. protected internal
  168. override void RenderContents (HtmlTextWriter writer)
  169. {
  170. TableRowSection currentTableSection = TableRowSection.TableHeader;
  171. TableRowSection rowSection;
  172. bool sectionStarted = false;
  173. if (Rows.Count > 0) {
  174. foreach (TableRow row in Rows) {
  175. if (generateTableSections) {
  176. rowSection = row.TableSection;
  177. if (rowSection < currentTableSection)
  178. throw new HttpException ("The table " + ID + " must contain row sections in order of header, body, then footer.");
  179. if (currentTableSection != rowSection) {
  180. if (sectionStarted) {
  181. writer.RenderEndTag ();
  182. sectionStarted = false;
  183. }
  184. currentTableSection = rowSection;
  185. }
  186. if (!sectionStarted) {
  187. switch (rowSection) {
  188. case TableRowSection.TableHeader:
  189. writer.RenderBeginTag (HtmlTextWriterTag.Thead);
  190. break;
  191. case TableRowSection.TableBody:
  192. writer.RenderBeginTag (HtmlTextWriterTag.Tbody);
  193. break;
  194. case TableRowSection.TableFooter:
  195. writer.RenderBeginTag (HtmlTextWriterTag.Tfoot);
  196. break;
  197. }
  198. sectionStarted = true;
  199. }
  200. }
  201. if (row != null)
  202. row.RenderControl (writer);
  203. }
  204. if (sectionStarted)
  205. writer.RenderEndTag ();
  206. }
  207. }
  208. // new in Fx 1.1 SP1 (to support Caption and CaptionAlign)
  209. public override void RenderBeginTag (HtmlTextWriter writer)
  210. {
  211. base.RenderBeginTag (writer);
  212. string s = Caption;
  213. if (s.Length > 0) {
  214. TableCaptionAlign tca = CaptionAlign;
  215. if (tca != TableCaptionAlign.NotSet)
  216. writer.AddAttribute (HtmlTextWriterAttribute.Align, tca.ToString ());
  217. writer.RenderBeginTag (HtmlTextWriterTag.Caption);
  218. writer.Write (s);
  219. writer.RenderEndTag ();
  220. }
  221. }
  222. void IPostBackEventHandler.RaisePostBackEvent (string argument)
  223. {
  224. RaisePostBackEvent (argument);
  225. }
  226. protected virtual void RaisePostBackEvent (string argument)
  227. {
  228. ValidateEvent (UniqueID, argument);
  229. }
  230. // inner class
  231. protected class RowControlCollection : ControlCollection {
  232. internal RowControlCollection (Table owner)
  233. : base (owner)
  234. {
  235. }
  236. public override void Add (Control child)
  237. {
  238. if (child == null)
  239. throw new NullReferenceException ("null");
  240. if (!(child is TableRow))
  241. throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
  242. base.Add (child);
  243. }
  244. public override void AddAt (int index, Control child)
  245. {
  246. if (child == null)
  247. throw new NullReferenceException ("null");
  248. if (!(child is TableRow))
  249. throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
  250. base.AddAt (index, child);
  251. }
  252. }
  253. }
  254. }