Table.cs 8.9 KB

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