Table.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // System.Web.UI.WebControls.Table.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Drawing;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.ComponentModel;
  37. using System.ComponentModel.Design;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [DefaultProperty("Rows")]
  41. [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  42. [ParseChildren(true, "Rows")]
  43. public class Table: WebControl
  44. {
  45. private TableRowCollection rows;
  46. private class TableRowControlCollection : ControlCollection
  47. {
  48. public TableRowControlCollection (Control owner) : base (owner)
  49. {
  50. }
  51. public override void Add (Control child)
  52. {
  53. if (!(child is TableRow))
  54. throw new ArgumentException (HttpRuntime.FormatResourceString (
  55. "Cannot_Have_Children_Of_Type",
  56. "Table",
  57. child.GetType ().Name.ToString ()));
  58. base.Add (child);
  59. }
  60. public override void AddAt(int index, Control child)
  61. {
  62. if (!(child is TableRow))
  63. throw new ArgumentException (HttpRuntime.FormatResourceString (
  64. "Cannot_Have_Children_Of_Type",
  65. "Table",
  66. child.GetType ().Name.ToString ()));
  67. base.AddAt (index, child);
  68. }
  69. }
  70. public Table () : base (HtmlTextWriterTag.Table)
  71. {
  72. }
  73. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  74. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  75. [WebSysDescription ("An Url specifying the background image for the table.")]
  76. public virtual string BackImageUrl
  77. {
  78. get {
  79. if (ControlStyleCreated)
  80. return ((TableStyle) ControlStyle).BackImageUrl;
  81. return String.Empty;
  82. }
  83. set { ((TableStyle) ControlStyle).BackImageUrl = value; }
  84. }
  85. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  86. [WebSysDescription ("The space left around the borders within a cell.")]
  87. public virtual int CellPadding
  88. {
  89. get {
  90. if (ControlStyleCreated)
  91. return ((TableStyle) ControlStyle).CellPadding;
  92. return -1;
  93. }
  94. set { ((TableStyle) ControlStyle).CellPadding = value; }
  95. }
  96. [DefaultValue (-1), Bindable (true), WebCategory ("Appearance")]
  97. [WebSysDescription ("The space left between cells.")]
  98. public virtual int CellSpacing
  99. {
  100. get {
  101. if (ControlStyleCreated)
  102. return ((TableStyle) ControlStyle).CellSpacing;
  103. return -1;
  104. }
  105. set { ((TableStyle) ControlStyle).CellSpacing = value; }
  106. }
  107. [DefaultValue (typeof (GridLines), "None"), Bindable (true), WebCategory ("Appearance")]
  108. [WebSysDescription ("The type of grid that a table uses.")]
  109. public virtual GridLines GridLines
  110. {
  111. get {
  112. if (ControlStyleCreated)
  113. return ((TableStyle) ControlStyle).GridLines;
  114. return GridLines.None;
  115. }
  116. set { ((TableStyle) ControlStyle).GridLines = value; }
  117. }
  118. [DefaultValue (typeof (HorizontalAlign), "NotSet"), Bindable (true), WebCategory ("Layout")]
  119. [WebSysDescription ("The horizonal alignment of the table.")]
  120. public virtual HorizontalAlign HorizontalAlign
  121. {
  122. get {
  123. if (ControlStyleCreated)
  124. return ((TableStyle) ControlStyle).HorizontalAlign;
  125. return HorizontalAlign.NotSet;
  126. }
  127. set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
  128. }
  129. [MergableProperty (false), PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  130. [WebSysDescription ("The rows of this table.")]
  131. public virtual TableRowCollection Rows
  132. {
  133. get {
  134. if (rows == null)
  135. rows = new TableRowCollection (this);
  136. return rows;
  137. }
  138. }
  139. protected override void AddAttributesToRender (HtmlTextWriter writer)
  140. {
  141. base.AddAttributesToRender (writer);
  142. if(!BorderColor.IsEmpty)
  143. writer.AddAttribute (HtmlTextWriterAttribute.Bordercolor,
  144. ColorTranslator.ToHtml (BorderColor));
  145. Unit bw = BorderWidth;
  146. if (GridLines == GridLines.None)
  147. bw = Unit.Pixel (0);
  148. else if (bw.IsEmpty || bw.Type != UnitType.Pixel)
  149. bw = Unit.Pixel(1);
  150. writer.AddAttribute (HtmlTextWriterAttribute.Border,
  151. ((int) bw.Value).ToString (NumberFormatInfo.InvariantInfo));
  152. }
  153. protected override ControlCollection CreateControlCollection ()
  154. {
  155. return new TableRowControlCollection (this);
  156. }
  157. protected override Style CreateControlStyle ()
  158. {
  159. return new TableStyle (ViewState);
  160. }
  161. protected override void RenderContents (HtmlTextWriter writer)
  162. {
  163. foreach (TableRow current in Rows)
  164. current.RenderControl (writer);
  165. }
  166. }
  167. }