2 // System.Web.UI.WebControls.DataList.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.Collections;
30 using System.ComponentModel;
31 using System.Globalization;
32 using System.Security.Permissions;
33 using System.Web.Util;
35 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [Designer ("System.Web.UI.Design.WebControls.DataListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [ControlValueProperty ("SelectedValue")]
44 [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, "System.ComponentModel.ComponentEditor, " + Consts.AssemblySystem)]
46 [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, typeof (System.ComponentModel.ComponentEditor))]
48 public class DataList : BaseDataList, INamingContainer, IRepeatInfoUser {
50 public const string CancelCommandName = "Cancel";
51 public const string DeleteCommandName = "Delete";
52 public const string EditCommandName = "Edit";
53 public const string SelectCommandName = "Select";
54 public const string UpdateCommandName = "Update";
57 private static readonly object cancelCommandEvent = new object ();
58 private static readonly object deleteCommandEvent = new object ();
59 private static readonly object editCommandEvent = new object ();
60 private static readonly object itemCommandEvent = new object ();
61 private static readonly object itemCreatedEvent = new object ();
62 private static readonly object itemDataBoundEvent = new object ();
63 private static readonly object updateCommandEvent = new object ();
65 private TableItemStyle alternatingItemStyle;
66 private TableItemStyle editItemStyle;
67 private TableItemStyle footerStyle;
68 private TableItemStyle headerStyle;
69 private TableItemStyle itemStyle;
70 private TableItemStyle selectedItemStyle;
71 private TableItemStyle separatorStyle;
73 private ITemplate alternatingItemTemplate;
74 private ITemplate editItemTemplate;
75 private ITemplate footerTemplate;
76 private ITemplate headerTemplate;
77 private ITemplate itemTemplate;
78 private ITemplate selectedItemTemplate;
79 private ITemplate separatorTemplate;
81 private DataListItemCollection items;
82 private ArrayList list;
86 //private int editIndex;
87 private int selectedIndex;
103 [DefaultValue (null)]
104 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
105 [NotifyParentProperty (true)]
106 [PersistenceMode (PersistenceMode.InnerProperty)]
107 [WebSysDescription ("")]
108 [WebCategory ("Style")]
109 public virtual TableItemStyle AlternatingItemStyle {
111 if (alternatingItemStyle == null) {
112 alternatingItemStyle = new TableItemStyle ();
113 if (IsTrackingViewState) {
114 alternatingItemStyle.TrackViewState ();
117 return alternatingItemStyle;
122 [DefaultValue (null)]
123 [TemplateContainer (typeof (DataListItem))]
124 [PersistenceMode (PersistenceMode.InnerProperty)]
125 [WebSysDescription ("")]
126 [WebCategory ("Style")]
127 public virtual ITemplate AlternatingItemTemplate {
128 get { return alternatingItemTemplate; }
129 set { alternatingItemTemplate = value; }
133 [WebSysDescription ("")]
134 [WebCategory ("Misc")]
135 public virtual int EditItemIndex {
137 object o = ViewState ["EditItemIndex"];
138 return (o == null) ? -1 : (int) o;
142 throw new ArgumentOutOfRangeException ("EditItemIndex", "< -1");
143 ViewState ["EditItemIndex"] = value;
147 [DefaultValue (null)]
148 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
149 [NotifyParentProperty (true)]
150 [PersistenceMode (PersistenceMode.InnerProperty)]
151 [WebSysDescription ("")]
152 [WebCategory ("Style")]
153 public virtual TableItemStyle EditItemStyle {
155 if (editItemStyle == null) {
156 editItemStyle = new TableItemStyle ();
157 if (IsTrackingViewState) {
158 editItemStyle.TrackViewState ();
161 return editItemStyle;
166 [DefaultValue (null)]
167 [TemplateContainer (typeof (DataListItem))]
168 [PersistenceMode (PersistenceMode.InnerProperty)]
169 [WebSysDescription ("")]
170 [WebCategory ("Style")]
171 public virtual ITemplate EditItemTemplate {
172 get { return editItemTemplate; }
173 set { editItemTemplate = value; }
176 [DefaultValue (false)]
177 [WebSysDescription ("")]
178 [WebCategory ("Misc")]
179 public virtual bool ExtractTemplateRows {
181 object o = ViewState ["ExtractTemplateRows"];
182 return (o == null) ? false : (bool) o;
184 set { ViewState ["ExtractTemplateRows"] = value; }
187 [DefaultValue (null)]
188 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
189 [NotifyParentProperty (true)]
190 [PersistenceMode (PersistenceMode.InnerProperty)]
191 [WebSysDescription ("")]
192 [WebCategory ("Style")]
193 public virtual TableItemStyle FooterStyle {
195 if (footerStyle == null) {
196 footerStyle = new TableItemStyle ();
197 if (IsTrackingViewState) {
198 footerStyle.TrackViewState ();
206 [DefaultValue (null)]
207 [TemplateContainer (typeof (DataListItem))]
208 [PersistenceMode (PersistenceMode.InnerProperty)]
209 [WebSysDescription ("")]
210 [WebCategory ("Style")]
211 public virtual ITemplate FooterTemplate {
212 get { return footerTemplate; }
213 set { footerTemplate = value; }
216 // yes! they do NOT match in fx1.1
217 [DefaultValue (GridLines.None)]
218 public override GridLines GridLines {
220 if (!ControlStyleCreated) {
222 return GridLines.None;
224 return GridLines.Both;
227 return TableStyle.GridLines;
229 set { TableStyle.GridLines = value; }
232 [DefaultValue (null)]
233 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
234 [NotifyParentProperty (true)]
235 [PersistenceMode (PersistenceMode.InnerProperty)]
236 [WebSysDescription ("")]
237 [WebCategory ("Style")]
238 public virtual TableItemStyle HeaderStyle {
240 if (headerStyle == null) {
241 headerStyle = new TableItemStyle ();
242 if (IsTrackingViewState) {
243 headerStyle.TrackViewState ();
251 [DefaultValue (null)]
252 [TemplateContainer (typeof (DataListItem))]
253 [PersistenceMode (PersistenceMode.InnerProperty)]
254 [WebSysDescription ("")]
255 [WebCategory ("Style")]
256 public virtual ITemplate HeaderTemplate {
257 get { return headerTemplate; }
258 set { headerTemplate = value; }
262 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
263 [WebSysDescription ("")]
264 [WebCategory ("Style")]
265 public virtual DataListItemCollection Items {
268 items = new DataListItemCollection (ItemList);
273 [DefaultValue (null)]
274 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
275 [NotifyParentProperty (true)]
276 [PersistenceMode (PersistenceMode.InnerProperty)]
277 [WebSysDescription ("")]
278 [WebCategory ("Style")]
279 public virtual TableItemStyle ItemStyle {
281 if (itemStyle == null) {
282 itemStyle = new TableItemStyle ();
283 if (IsTrackingViewState) {
284 itemStyle.TrackViewState ();
292 [DefaultValue (null)]
293 [TemplateContainer (typeof (DataListItem))]
294 [PersistenceMode (PersistenceMode.InnerProperty)]
295 [WebSysDescription ("")]
296 [WebCategory ("Style")]
297 public virtual ITemplate ItemTemplate {
298 get { return itemTemplate; }
299 set { itemTemplate = value; }
306 [WebSysDescription ("")]
307 [WebCategory ("Layout")]
308 public virtual int RepeatColumns {
310 object o = ViewState ["RepeatColumns"];
311 return (o == null) ? 0 : (int) o;
315 throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
317 ViewState ["RepeatColumns"] = value;
324 [DefaultValue (RepeatDirection.Vertical)]
325 [WebSysDescription ("")]
326 [WebCategory ("Layout")]
327 public virtual RepeatDirection RepeatDirection {
329 object o = ViewState ["RepeatDirection"];
330 return (o == null) ? RepeatDirection.Vertical : (RepeatDirection) o;
332 set { ViewState ["RepeatDirection"] = value; }
338 [DefaultValue (RepeatLayout.Table)]
339 [WebSysDescription ("")]
340 [WebCategory ("Layout")]
341 public virtual RepeatLayout RepeatLayout {
343 object o = ViewState ["RepeatLayout"];
344 return (o == null) ? RepeatLayout.Table : (RepeatLayout) o;
346 set { ViewState ["RepeatLayout"] = value; }
351 [WebSysDescription ("")]
352 [WebCategory ("Layout")]
353 public virtual int SelectedIndex {
355 object o = ViewState ["SelectedIndex"];
356 return (o == null) ? -1 : (int) o;
360 throw new ArgumentOutOfRangeException ("SelectedIndex", "< -1");
361 ViewState ["SelectedIndex"] = value;
366 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
367 [WebSysDescription ("")]
368 [WebCategory ("Layout")]
369 public virtual DataListItem SelectedItem {
371 if (SelectedIndex < 0)
373 if (SelectedIndex >= Items.Count)
374 throw new ArgumentOutOfRangeException ("SelectedItem", ">= Items.Count");
375 return items [SelectedIndex];
379 [DefaultValue (null)]
380 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
381 [NotifyParentProperty (true)]
382 [PersistenceMode (PersistenceMode.InnerProperty)]
383 [WebSysDescription ("")]
384 [WebCategory ("Style")]
385 public virtual TableItemStyle SelectedItemStyle {
387 if (selectedItemStyle == null) {
388 selectedItemStyle = new TableItemStyle ();
389 if (IsTrackingViewState) {
390 selectedItemStyle.TrackViewState ();
393 return selectedItemStyle;
398 [DefaultValue (null)]
399 [TemplateContainer (typeof (DataListItem))]
400 [PersistenceMode (PersistenceMode.InnerProperty)]
401 [WebSysDescription ("")]
402 [WebCategory ("Style")]
403 public virtual ITemplate SelectedItemTemplate {
404 get { return selectedItemTemplate; }
405 set { selectedItemTemplate = value; }
408 [DefaultValue (null)]
409 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
410 [NotifyParentProperty (true)]
411 [PersistenceMode (PersistenceMode.InnerProperty)]
412 [WebSysDescription ("")]
413 [WebCategory ("Style")]
414 public virtual TableItemStyle SeparatorStyle {
416 if (separatorStyle == null) {
417 separatorStyle = new TableItemStyle ();
418 if (IsTrackingViewState) {
419 separatorStyle.TrackViewState ();
422 return separatorStyle;
427 [DefaultValue (null)]
428 [TemplateContainer (typeof (DataListItem))]
429 [PersistenceMode (PersistenceMode.InnerProperty)]
430 [WebSysDescription ("")]
431 [WebCategory ("Style")]
432 public virtual ITemplate SeparatorTemplate {
433 get { return separatorTemplate; }
434 set { separatorTemplate = value; }
440 [DefaultValue (true)]
441 [WebSysDescription ("")]
442 [WebCategory ("Appearance")]
443 public virtual bool ShowFooter {
445 object o = ViewState ["ShowFooter"];
446 return (o == null) ? true : (bool) o;
448 set { ViewState ["ShowFooter"] = value; }
454 [DefaultValue (true)]
455 [WebSysDescription ("")]
456 [WebCategory ("Appearance")]
457 public virtual bool ShowHeader {
459 object o = ViewState ["ShowHeader"];
460 return (o == null) ? true : (bool) o;
462 set { ViewState ["ShowHeader"] = value; }
466 [MonoTODO ("incomplete")]
468 public object SelectedValue {
470 if (DataKeyField.Length == 0)
471 throw new InvalidOperationException (Locale.GetText ("No DataKeyField present."));
473 if ((SelectedIndex >= 0) && (selectedIndex < DataKeys.Count)) {
474 return DataKeys [selectedIndex];
481 protected override HtmlTextWriterTag TagKey {
482 get { return HtmlTextWriterTag.Table; }
486 private TableStyle TableStyle {
487 // this will throw an InvalidCasException just like we need
488 get { return (TableStyle) ControlStyle; }
491 private ArrayList ItemList {
494 list = new ArrayList ();
499 void DoItem (int i, ListItemType t, object d, bool databind)
501 DataListItem itm = CreateItem (i, t);
504 DataListItemEventArgs e = new DataListItemEventArgs (itm);
505 InitializeItem (itm);
508 // It is very important that this be called *before* data
509 // binding. Otherwise, we won't save our state in the viewstate.
524 private void DoItemInLoop (int i, object d, bool databind, ListItemType type)
526 DoItem (i, type, d, databind);
527 if (SeparatorTemplate != null)
528 DoItem (i, ListItemType.Separator, null, databind);
532 protected override void CreateControlHierarchy (bool useDataSource)
537 IEnumerable ds = null;
538 ArrayList keys = null;
543 if (IsBoundUsingDataSourceID)
547 ds = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
548 keys = DataKeysArray;
551 idx = (int) ViewState ["Items"];
554 if ((ds == null) && (idx == 0))
557 if (headerTemplate != null)
558 DoItem (-1, ListItemType.Header, null, useDataSource);
561 int selected_index = SelectedIndex;
562 int edit_item_index = EditItemIndex;
565 string key = DataKeyField;
566 foreach (object o in ds) {
567 if (useDataSource && key != "")
568 keys.Add (DataBinder.GetPropertyValue (o, key));
569 type = ListItemType.Item;
570 if (idx == edit_item_index)
571 type = ListItemType.EditItem;
572 else if (idx == selected_index)
573 type = ListItemType.SelectedItem;
574 else if ((idx & 1) != 0)
575 type = ListItemType.AlternatingItem;
577 DoItemInLoop (idx, o, useDataSource, type);
581 for (int i = 0; i < idx; i++) {
582 type = ListItemType.Item;
583 if (i == edit_item_index)
584 type = ListItemType.EditItem;
585 else if (i == selected_index)
586 type = ListItemType.SelectedItem;
587 else if ((i & 1) != 0)
588 type = ListItemType.AlternatingItem;
590 DoItemInLoop (i, null, useDataSource, type);
594 if (footerTemplate != null)
595 DoItem (-1, ListItemType.Footer, null, useDataSource);
597 ViewState ["Items"] = idx;
600 protected override Style CreateControlStyle ()
603 // not kept (directly) in the DataList ViewState
604 TableStyle tableStyle = new TableStyle ();
606 TableStyle tableStyle = new TableStyle (ViewState);
608 tableStyle.CellSpacing = 0;
612 protected virtual DataListItem CreateItem (int itemIndex, ListItemType itemType)
614 return new DataListItem (itemIndex, itemType);
617 protected virtual void InitializeItem (DataListItem item)
621 switch (item.ItemType) {
622 case ListItemType.Header:
625 case ListItemType.Footer:
628 case ListItemType.Separator:
629 t = SeparatorTemplate;
631 case ListItemType.Item:
632 case ListItemType.AlternatingItem:
633 case ListItemType.SelectedItem:
634 case ListItemType.EditItem:
635 int index = item.ItemIndex;
636 if ((item.ItemType == ListItemType.EditItem) && (EditItemTemplate != null))
637 t = EditItemTemplate;
638 else if ((item.ItemType == ListItemType.SelectedItem) && (SelectedItemTemplate != null))
639 t = SelectedItemTemplate;
640 else if ((item.ItemType == ListItemType.AlternatingItem) && (AlternatingItemTemplate != null))
641 t = AlternatingItemTemplate;
648 t.InstantiateIn (item);
651 protected override void LoadViewState (object savedState)
653 object[] state = (object[]) savedState;
654 base.LoadViewState (state [0]);
655 if (state [1] != null)
656 ItemStyle.LoadViewState (state [1]);
657 if (state [2] != null)
658 SelectedItemStyle.LoadViewState (state [2]);
659 if (state [3] != null)
660 AlternatingItemStyle.LoadViewState (state [3]);
661 if (state [4] != null)
662 EditItemStyle.LoadViewState (state [4]);
663 if (state [5] != null)
664 SeparatorStyle.LoadViewState (state [5]);
665 if (state [6] != null)
666 HeaderStyle.LoadViewState (state [6]);
667 if (state [7] != null)
668 FooterStyle.LoadViewState (state [7]);
670 if (state [8] != null)
671 ControlStyle.LoadViewState (state [8]);
675 protected override bool OnBubbleEvent (object source, EventArgs e)
677 DataListCommandEventArgs dlca = (e as DataListCommandEventArgs);
681 string cn = dlca.CommandName;
682 CultureInfo inv = CultureInfo.InvariantCulture;
684 OnItemCommand (dlca);
685 if (String.Compare (cn, CancelCommandName, true, inv) == 0) {
686 OnCancelCommand (dlca);
687 } else if (String.Compare (cn, DeleteCommandName, true, inv) == 0) {
688 OnDeleteCommand (dlca);
689 } else if (String.Compare (cn, EditCommandName, true, inv) == 0) {
690 OnEditCommand (dlca);
691 } else if (String.Compare (cn, SelectCommandName, true, inv) == 0) {
692 SelectedIndex = dlca.Item.ItemIndex;
693 OnSelectedIndexChanged (dlca);
694 } else if (String.Compare (cn, UpdateCommandName, true, inv) == 0) {
695 OnUpdateCommand (dlca);
700 protected virtual void OnCancelCommand (DataListCommandEventArgs e)
702 DataListCommandEventHandler cancelCommand = (DataListCommandEventHandler) Events [cancelCommandEvent];
703 if (cancelCommand != null)
704 cancelCommand (this, e);
707 protected virtual void OnDeleteCommand (DataListCommandEventArgs e)
709 DataListCommandEventHandler deleteCommand = (DataListCommandEventHandler) Events [deleteCommandEvent];
710 if (deleteCommand != null)
711 deleteCommand (this, e);
714 protected virtual void OnEditCommand (DataListCommandEventArgs e)
716 DataListCommandEventHandler editCommand = (DataListCommandEventHandler) Events [editCommandEvent];
717 if (editCommand != null)
718 editCommand (this, e);
721 protected internal override void OnInit (EventArgs e)
723 // EditItemIndex and SelectedIndex now use the Control State (i.e not the View State)
724 Page.RegisterRequiresControlState (this);
728 protected virtual void OnItemCommand (DataListCommandEventArgs e)
730 DataListCommandEventHandler itemCommand = (DataListCommandEventHandler) Events [itemCommandEvent];
731 if (itemCommand != null)
732 itemCommand (this, e);
735 protected virtual void OnItemCreated (DataListItemEventArgs e)
737 DataListItemEventHandler itemCreated = (DataListItemEventHandler) Events [itemCreatedEvent];
738 if (itemCreated != null)
739 itemCreated (this, e);
742 protected virtual void OnItemDataBound (DataListItemEventArgs e)
744 DataListItemEventHandler itemDataBound = (DataListItemEventHandler) Events [itemDataBoundEvent];
745 if (itemDataBound != null)
746 itemDataBound (this, e);
749 protected virtual void OnUpdateCommand (DataListCommandEventArgs e)
751 DataListCommandEventHandler updateCommand = (DataListCommandEventHandler) Events [updateCommandEvent];
752 if (updateCommand != null)
753 updateCommand (this, e);
756 protected override void PrepareControlHierarchy ()
758 if (!HasControls () || Controls.Count == 0)
759 return; // No one called CreateControlHierarchy() with DataSource != null
762 foreach (DataListItem item in Controls) {
763 switch (item.ItemType) {
764 case ListItemType.Item:
765 item.MergeStyle (itemStyle);
767 case ListItemType.AlternatingItem:
769 if (alternatingItemStyle != null) {
770 alt = new TableItemStyle ();
771 alt.CopyFrom (itemStyle);
772 alt.CopyFrom (alternatingItemStyle);
778 item.MergeStyle (alt);
780 case ListItemType.EditItem:
781 if (editItemStyle != null) {
782 item.MergeStyle (editItemStyle);
784 item.MergeStyle (itemStyle);
787 case ListItemType.Footer:
789 item.Visible = false;
792 if (footerStyle != null) {
793 item.MergeStyle (footerStyle);
796 case ListItemType.Header:
798 item.Visible = false;
801 if (headerStyle != null) {
802 item.MergeStyle (headerStyle);
805 case ListItemType.SelectedItem:
806 if (selectedItemStyle != null) {
807 item.MergeStyle (selectedItemStyle);
809 item.MergeStyle (itemStyle);
812 case ListItemType.Separator:
813 if (separatorStyle != null) {
814 item.MergeStyle(separatorStyle);
817 item.MergeStyle (itemStyle);
829 override void RenderContents (HtmlTextWriter writer)
831 if (Items.Count == 0)
834 RepeatInfo ri = new RepeatInfo ();
835 ri.RepeatColumns = RepeatColumns;
836 ri.RepeatDirection = RepeatDirection;
837 ri.RepeatLayout = RepeatLayout;
838 ri.CaptionAlign = CaptionAlign;
839 ri.Caption = Caption;
840 ri.UseAccessibleHeader = UseAccessibleHeader;
842 // debugging stuff that I prefer to keep for a while
843 Console.WriteLine ("RepeatColumns {0}", ri.RepeatColumns);
844 Console.WriteLine ("RepeatDirection {0}", ri.RepeatDirection);
845 Console.WriteLine ("RepeatLayout {0}", ri.RepeatLayout);
846 Console.WriteLine ("OuterTableImplied {0}", ExtractTemplateRows);
847 Console.WriteLine ("IRepeatInfoUser.HasFooter {0}", (ShowFooter && (footerTemplate != null)));
848 Console.WriteLine ("IRepeatInfoUser.HasHeader {0}", (ShowHeader && (headerTemplate != null)));
849 Console.WriteLine ("IRepeatInfoUser.HasSeparators {0}", (separatorTemplate != null));
850 Console.WriteLine ("IRepeatInfoUser.RepeatedItemCount {0}", Items.Count);
851 for (int i=0; i < Items.Count; i++) {
852 DataListItem dli = Items [i];
853 Console.WriteLine ("{0}: Index {1}, Type {2}", i, dli.ItemIndex, dli.ItemType);
856 bool extract = ExtractTemplateRows;
858 ri.OuterTableImplied = true;
859 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
860 if (ControlStyleCreated) {
861 ControlStyle.AddAttributesToRender (writer);
863 writer.RenderBeginTag (HtmlTextWriterTag.Table);
864 ri.RenderRepeater (writer, this, ControlStyle, this);
865 writer.RenderEndTag ();
867 ri.RenderRepeater (writer, this, ControlStyle, this);
871 protected override object SaveViewState ()
874 object[] state = new object [9];
876 object[] state = new object [8];
878 state[0] = base.SaveViewState ();
879 if (itemStyle != null)
880 state [1] = itemStyle.SaveViewState ();
881 if (selectedItemStyle != null)
882 state [2] = selectedItemStyle.SaveViewState ();
883 if (alternatingItemStyle != null)
884 state [3] = alternatingItemStyle.SaveViewState ();
885 if (editItemStyle != null)
886 state [4] = editItemStyle.SaveViewState ();
887 if (separatorStyle != null)
888 state [5] = separatorStyle.SaveViewState ();
889 if (headerStyle != null)
890 state [6] = headerStyle.SaveViewState ();
891 if (footerStyle != null)
892 state [7] = footerStyle.SaveViewState ();
894 if (ControlStyleCreated)
895 state [8] = ControlStyle.SaveViewState ();
900 protected override void TrackViewState ()
902 base.TrackViewState ();
903 if (alternatingItemStyle != null)
904 alternatingItemStyle.TrackViewState ();
905 if (editItemStyle != null)
906 editItemStyle.TrackViewState ();
907 if (footerStyle != null)
908 footerStyle.TrackViewState ();
909 if (headerStyle != null)
910 headerStyle.TrackViewState ();
911 if (itemStyle != null)
912 itemStyle.TrackViewState ();
913 if (selectedItemStyle != null)
914 selectedItemStyle.TrackViewState ();
915 if (separatorStyle != null)
916 separatorStyle.TrackViewState ();
918 if (ControlStyleCreated)
919 ControlStyle.TrackViewState ();
924 [WebSysDescription ("")]
925 [WebCategory ("Action")]
926 public event DataListCommandEventHandler CancelCommand {
927 add { Events.AddHandler (cancelCommandEvent, value); }
928 remove { Events.RemoveHandler (cancelCommandEvent, value); }
931 [WebSysDescription ("")]
932 [WebCategory ("Action")]
933 public event DataListCommandEventHandler DeleteCommand {
934 add { Events.AddHandler (deleteCommandEvent, value); }
935 remove { Events.RemoveHandler (deleteCommandEvent, value); }
938 [WebSysDescription ("")]
939 [WebCategory ("Action")]
940 public event DataListCommandEventHandler EditCommand {
941 add { Events.AddHandler (editCommandEvent, value); }
942 remove { Events.RemoveHandler (editCommandEvent, value); }
945 [WebSysDescription ("")]
946 [WebCategory ("Action")]
947 public event DataListCommandEventHandler ItemCommand {
948 add { Events.AddHandler (itemCommandEvent, value); }
949 remove { Events.RemoveHandler (itemCommandEvent, value); }
952 [WebSysDescription ("")]
953 [WebCategory ("Action")]
954 public event DataListItemEventHandler ItemCreated {
955 add { Events.AddHandler (itemCreatedEvent, value); }
956 remove { Events.RemoveHandler (itemCreatedEvent, value); }
959 [WebSysDescription ("")]
960 [WebCategory ("Action")]
961 public event DataListItemEventHandler ItemDataBound {
962 add { Events.AddHandler (itemDataBoundEvent, value); }
963 remove { Events.RemoveHandler (itemDataBoundEvent, value); }
966 [WebSysDescription ("")]
967 [WebCategory ("Action")]
968 public event DataListCommandEventHandler UpdateCommand {
969 add { Events.AddHandler (updateCommandEvent, value); }
970 remove { Events.RemoveHandler (updateCommandEvent, value); }
974 // IRepeatInfoUser support
976 bool IRepeatInfoUser.HasFooter {
977 get { return (ShowFooter && (footerTemplate != null)); }
980 bool IRepeatInfoUser.HasHeader {
981 get { return (ShowHeader && (headerTemplate != null)); }
984 bool IRepeatInfoUser.HasSeparators {
985 get { return (separatorTemplate != null); }
988 // don't include header, footer and separators in the count
989 int IRepeatInfoUser.RepeatedItemCount {
992 object o = ViewState ["Items"];
993 idx = (o == null) ? 0 : (int) o;
999 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
1001 DataListItem item = null;
1003 case ListItemType.Header:
1004 case ListItemType.Footer:
1005 if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
1006 throw new ArgumentOutOfRangeException ();
1008 item = FindFirstItem (itemType);
1010 case ListItemType.Item:
1011 case ListItemType.AlternatingItem:
1012 case ListItemType.SelectedItem:
1013 case ListItemType.EditItem:
1014 if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
1015 throw new ArgumentOutOfRangeException ();
1017 item = FindBestItem (repeatIndex);
1019 case ListItemType.Separator:
1020 if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
1021 throw new ArgumentOutOfRangeException ();
1023 item = FindSpecificItem (itemType, repeatIndex);
1030 if (item == null || item.ControlStyleCreated == false)
1033 return item.ControlStyle;
1036 // Header and Footer don't have a "real" index (-1)
1037 private DataListItem FindFirstItem (ListItemType itemType)
1039 for (int i = 0; i < Controls.Count; i++) {
1040 DataListItem item = (Controls [i] as DataListItem);
1041 if ((item != null) && (item.ItemType == itemType))
1047 // Both Type and Index must match (e.g. Separator)
1048 private DataListItem FindSpecificItem (ListItemType itemType, int repeatIndex)
1050 for (int i = 0; i < Controls.Count; i++) {
1051 DataListItem item = (Controls [i] as DataListItem);
1052 if ((item != null) && (item.ItemType == itemType) && (item.ItemIndex == repeatIndex))
1058 // we get call for Item even for AlternatingItem :(
1059 private DataListItem FindBestItem (int repeatIndex)
1061 for (int i = 0; i < Controls.Count; i++) {
1062 DataListItem item = (Controls [i] as DataListItem);
1063 if ((item != null) && (item.ItemIndex == repeatIndex)) {
1064 switch (item.ItemType) {
1065 case ListItemType.Item:
1066 case ListItemType.AlternatingItem:
1067 case ListItemType.SelectedItem:
1068 case ListItemType.EditItem:
1078 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
1080 // if possible take the easy way out...
1081 if (!HasControls ())
1084 DataListItem item = null;
1086 case ListItemType.Header:
1087 case ListItemType.Footer:
1088 item = FindFirstItem (itemType);
1090 case ListItemType.Item:
1091 case ListItemType.AlternatingItem:
1092 case ListItemType.SelectedItem:
1093 case ListItemType.EditItem:
1094 item = FindBestItem (repeatIndex);
1096 case ListItemType.Separator:
1097 item = FindSpecificItem (itemType, repeatIndex);
1102 bool extract = ExtractTemplateRows;
1103 bool table = (RepeatLayout == RepeatLayout.Table);
1104 if (!table || extract) {
1105 // sadly RepeatInfo doesn't support Style for RepeatLayout.Flow
1106 Style s = (this as IRepeatInfoUser).GetItemStyle (itemType, repeatIndex);
1108 item.ControlStyle.CopyFrom (s);
1110 //Console.WriteLine ("RenderItem #{0} type {1}", repeatIndex, itemType);
1111 item.RenderItem (writer, extract, table);
1113 //Console.WriteLine ("Couldn't find #{0} type {1} out of {2} items / {3} controls", repeatIndex, itemType, Items.Count, Controls.Count);