2 // System.Web.UI.WebControls.Table.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls {
35 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [DefaultProperty ("Rows")]
39 [Designer ("System.Web.UI.Design.WebControls.TableDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 [ParseChildren (true, "Rows")]
42 [SupportsEventValidation]
43 public class Table : WebControl, IPostBackEventHandler
46 public class Table : WebControl
49 TableRowCollection rows;
51 bool generateTableSections;
55 : base (HtmlTextWriterTag.Table)
60 internal bool GenerateTableSections {
61 get { return generateTableSections; }
62 set { generateTableSections = value; }
65 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
69 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
72 [WebSysDescription ("")]
73 [WebCategory ("Appearance")]
74 public virtual string BackImageUrl {
76 if (!ControlStyleCreated)
77 return String.Empty; // default value
78 return TableStyle.BackImageUrl;
80 set { TableStyle.BackImageUrl = value; }
83 // note: it seems that Caption and CaptionAlign appeared in 1.1 SP1
89 [WebSysDescription ("")]
90 [WebCategory ("Accessibility")]
91 public virtual string Caption {
93 object o = ViewState ["Caption"];
94 return (o == null) ? String.Empty : (string) o;
98 ViewState.Remove ("Caption");
100 ViewState ["Caption"] = value;
104 [DefaultValue (TableCaptionAlign.NotSet)]
105 [WebCategory ("Accessibility")]
106 public virtual TableCaptionAlign CaptionAlign {
108 object o = ViewState ["CaptionAlign"];
109 return (o == null) ? TableCaptionAlign.NotSet : (TableCaptionAlign) o;
112 if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right)) {
113 throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
115 ViewState ["CaptionAlign"] = value;
123 [WebSysDescription ("")]
124 [WebCategory ("Appearance")]
125 public virtual int CellPadding {
127 if (!ControlStyleCreated)
128 return -1; // default value
129 return TableStyle.CellPadding;
131 set { TableStyle.CellPadding = value; }
138 [WebSysDescription ("")]
139 [WebCategory ("Appearance")]
140 public virtual int CellSpacing {
142 if (!ControlStyleCreated)
143 return -1; // default value
144 return TableStyle.CellSpacing;
146 set { TableStyle.CellSpacing = value; }
152 [DefaultValue (GridLines.None)]
153 [WebSysDescription ("")]
154 [WebCategory ("Appearance")]
155 public virtual GridLines GridLines {
157 if (!ControlStyleCreated)
158 return GridLines.None; // default value
159 return TableStyle.GridLines;
161 set { TableStyle.GridLines = value; }
167 [DefaultValue (HorizontalAlign.NotSet)]
168 [WebSysDescription ("")]
169 [WebCategory ("Layout")]
170 public virtual HorizontalAlign HorizontalAlign {
172 if (!ControlStyleCreated)
173 return HorizontalAlign.NotSet; // default value
174 return TableStyle.HorizontalAlign;
176 set { TableStyle.HorizontalAlign = value; }
179 [MergableProperty (false)]
180 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
181 [WebSysDescription ("")]
182 public virtual TableRowCollection Rows {
185 rows = new TableRowCollection (this);
190 private TableStyle TableStyle {
191 get { return (ControlStyle as TableStyle); }
194 public override bool SupportsDisabledAttribute {
195 get { return RenderingCompatibilityLessThan40; }
198 protected override void AddAttributesToRender (HtmlTextWriter writer)
200 base.AddAttributesToRender (writer);
202 if (!ControlStyleCreated || TableStyle.IsEmpty) {
203 // for some reason border=X seems to be always present
204 // and isn't rendered as a style attribute
205 writer.AddAttribute (HtmlTextWriterAttribute.Border, "0", false);
210 protected override ControlCollection CreateControlCollection ()
212 return new RowControlCollection (this);
215 protected override Style CreateControlStyle ()
217 return new TableStyle (ViewState);
225 override void RenderContents (HtmlTextWriter writer)
228 TableRowSection currentTableSection = TableRowSection.TableHeader;
229 TableRowSection rowSection;
230 bool sectionStarted = false;
233 if (Rows.Count > 0) {
234 foreach (TableRow row in Rows) {
236 if (generateTableSections) {
237 rowSection = row.TableSection;
238 if (rowSection < currentTableSection)
239 throw new HttpException ("The table " + ID + " must contain row sections in order of header, body, then footer.");
241 if (currentTableSection != rowSection) {
242 if (sectionStarted) {
243 writer.RenderEndTag ();
244 sectionStarted = false;
247 currentTableSection = rowSection;
250 if (!sectionStarted) {
251 switch (rowSection) {
252 case TableRowSection.TableHeader:
253 writer.RenderBeginTag (HtmlTextWriterTag.Thead);
256 case TableRowSection.TableBody:
257 writer.RenderBeginTag (HtmlTextWriterTag.Tbody);
260 case TableRowSection.TableFooter:
261 writer.RenderBeginTag (HtmlTextWriterTag.Tfoot);
264 sectionStarted = true;
269 row.RenderControl (writer);
274 writer.RenderEndTag ();
280 // new in Fx 1.1 SP1 (to support Caption and CaptionAlign)
282 public override void RenderBeginTag (HtmlTextWriter writer)
284 base.RenderBeginTag (writer);
288 TableCaptionAlign tca = CaptionAlign;
289 if (tca != TableCaptionAlign.NotSet)
290 writer.AddAttribute (HtmlTextWriterAttribute.Align, tca.ToString ());
292 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
294 writer.RenderEndTag ();
297 // else if (HasControls ()) {
304 void IPostBackEventHandler.RaisePostBackEvent (string argument)
306 RaisePostBackEvent (argument);
309 protected virtual void RaisePostBackEvent (string argument)
311 ValidateEvent (UniqueID, argument);
316 protected class RowControlCollection : ControlCollection {
318 internal RowControlCollection (Table owner)
324 public override void Add (Control child)
327 throw new NullReferenceException ("null");
328 if (!(child is TableRow))
329 throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
334 public override void AddAt (int index, Control child)
337 throw new NullReferenceException ("null");
338 if (!(child is TableRow))
339 throw new ArgumentException ("child", Locale.GetText ("Must be an TableRow instance."));
341 base.AddAt (index, child);