2 // System.Web.UI.WebControls.DataList.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005-2010 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")]
42 [ControlValueProperty ("SelectedValue")]
43 [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts
.AssemblySystem_Design
, "System.ComponentModel.ComponentEditor, " + Consts
.AssemblySystem
)]
44 public class DataList
: BaseDataList
, INamingContainer
, IRepeatInfoUser
46 public const string CancelCommandName
= "Cancel";
47 public const string DeleteCommandName
= "Delete";
48 public const string EditCommandName
= "Edit";
49 public const string SelectCommandName
= "Select";
50 public const string UpdateCommandName
= "Update";
52 static readonly object cancelCommandEvent
= new object ();
53 static readonly object deleteCommandEvent
= new object ();
54 static readonly object editCommandEvent
= new object ();
55 static readonly object itemCommandEvent
= new object ();
56 static readonly object itemCreatedEvent
= new object ();
57 static readonly object itemDataBoundEvent
= new object ();
58 static readonly object updateCommandEvent
= new object ();
60 TableItemStyle alternatingItemStyle
;
61 TableItemStyle editItemStyle
;
62 TableItemStyle footerStyle
;
63 TableItemStyle headerStyle
;
64 TableItemStyle itemStyle
;
65 TableItemStyle selectedItemStyle
;
66 TableItemStyle separatorStyle
;
68 ITemplate alternatingItemTemplate
;
69 ITemplate editItemTemplate
;
70 ITemplate footerTemplate
;
71 ITemplate headerTemplate
;
72 ITemplate itemTemplate
;
73 ITemplate selectedItemTemplate
;
74 ITemplate separatorTemplate
;
76 DataListItemCollection items
;
86 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
87 [NotifyParentProperty (true)]
88 [PersistenceMode (PersistenceMode
.InnerProperty
)]
89 [WebSysDescription ("")]
90 [WebCategory ("Style")]
91 public virtual TableItemStyle AlternatingItemStyle
{
93 if (alternatingItemStyle
== null) {
94 alternatingItemStyle
= new TableItemStyle ();
95 if (IsTrackingViewState
)
96 alternatingItemStyle
.TrackViewState ();
98 return alternatingItemStyle
;
103 [DefaultValue (null)]
104 [TemplateContainer (typeof (DataListItem
))]
105 [PersistenceMode (PersistenceMode
.InnerProperty
)]
106 [WebSysDescription ("")]
107 [WebCategory ("Style")]
108 public virtual ITemplate AlternatingItemTemplate
{
109 get { return alternatingItemTemplate; }
110 set { alternatingItemTemplate = value; }
114 [WebSysDescription ("")]
115 [WebCategory ("Misc")]
116 public virtual int EditItemIndex
{
118 object o
= ViewState
["EditItemIndex"];
119 return (o
== null) ? -1 : (int) o
;
123 throw new ArgumentOutOfRangeException ("EditItemIndex", "< -1");
124 ViewState
["EditItemIndex"] = value;
128 [DefaultValue (null)]
129 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
130 [NotifyParentProperty (true)]
131 [PersistenceMode (PersistenceMode
.InnerProperty
)]
132 [WebSysDescription ("")]
133 [WebCategory ("Style")]
134 public virtual TableItemStyle EditItemStyle
{
136 if (editItemStyle
== null) {
137 editItemStyle
= new TableItemStyle ();
138 if (IsTrackingViewState
)
139 editItemStyle
.TrackViewState ();
141 return editItemStyle
;
146 [DefaultValue (null)]
147 [TemplateContainer (typeof (DataListItem
))]
148 [PersistenceMode (PersistenceMode
.InnerProperty
)]
149 [WebSysDescription ("")]
150 [WebCategory ("Style")]
151 public virtual ITemplate EditItemTemplate
{
152 get { return editItemTemplate; }
153 set { editItemTemplate = value; }
156 [DefaultValue (false)]
157 [WebSysDescription ("")]
158 [WebCategory ("Misc")]
159 public virtual bool ExtractTemplateRows
{
161 object o
= ViewState
["ExtractTemplateRows"];
162 return (o
== null) ? false : (bool) o
;
164 set { ViewState ["ExtractTemplateRows"] = value; }
167 [DefaultValue (null)]
168 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
169 [NotifyParentProperty (true)]
170 [PersistenceMode (PersistenceMode
.InnerProperty
)]
171 [WebSysDescription ("")]
172 [WebCategory ("Style")]
173 public virtual TableItemStyle FooterStyle
{
175 if (footerStyle
== null) {
176 footerStyle
= new TableItemStyle ();
177 if (IsTrackingViewState
)
178 footerStyle
.TrackViewState ();
185 [DefaultValue (null)]
186 [TemplateContainer (typeof (DataListItem
))]
187 [PersistenceMode (PersistenceMode
.InnerProperty
)]
188 [WebSysDescription ("")]
189 [WebCategory ("Style")]
190 public virtual ITemplate FooterTemplate
{
191 get { return footerTemplate; }
192 set { footerTemplate = value; }
195 // yes! they do NOT match in fx1.1
196 [DefaultValue (GridLines
.None
)]
197 public override GridLines GridLines
{
199 if (!ControlStyleCreated
)
200 return GridLines
.None
;
201 return TableStyle
.GridLines
;
203 set { TableStyle.GridLines = value; }
206 [DefaultValue (null)]
207 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
208 [NotifyParentProperty (true)]
209 [PersistenceMode (PersistenceMode
.InnerProperty
)]
210 [WebSysDescription ("")]
211 [WebCategory ("Style")]
212 public virtual TableItemStyle HeaderStyle
{
214 if (headerStyle
== null) {
215 headerStyle
= new TableItemStyle ();
216 if (IsTrackingViewState
)
217 headerStyle
.TrackViewState ();
224 [DefaultValue (null)]
225 [TemplateContainer (typeof (DataListItem
))]
226 [PersistenceMode (PersistenceMode
.InnerProperty
)]
227 [WebSysDescription ("")]
228 [WebCategory ("Style")]
229 public virtual ITemplate HeaderTemplate
{
230 get { return headerTemplate; }
231 set { headerTemplate = value; }
235 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
236 [WebSysDescription ("")]
237 [WebCategory ("Style")]
238 public virtual DataListItemCollection Items
{
241 items
= new DataListItemCollection (ItemList
);
246 [DefaultValue (null)]
247 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
248 [NotifyParentProperty (true)]
249 [PersistenceMode (PersistenceMode
.InnerProperty
)]
250 [WebSysDescription ("")]
251 [WebCategory ("Style")]
252 public virtual TableItemStyle ItemStyle
{
254 if (itemStyle
== null) {
255 itemStyle
= new TableItemStyle ();
256 if (IsTrackingViewState
)
257 itemStyle
.TrackViewState ();
264 [DefaultValue (null)]
265 [TemplateContainer (typeof (DataListItem
))]
266 [PersistenceMode (PersistenceMode
.InnerProperty
)]
267 [WebSysDescription ("")]
268 [WebCategory ("Style")]
269 public virtual ITemplate ItemTemplate
{
270 get { return itemTemplate; }
271 set { itemTemplate = value; }
275 [WebSysDescription ("")]
276 [WebCategory ("Layout")]
277 public virtual int RepeatColumns
{
279 object o
= ViewState
["RepeatColumns"];
280 return (o
== null) ? 0 : (int) o
;
284 throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
286 ViewState
["RepeatColumns"] = value;
290 [DefaultValue (RepeatDirection
.Vertical
)]
291 [WebSysDescription ("")]
292 [WebCategory ("Layout")]
293 public virtual RepeatDirection RepeatDirection
{
295 object o
= ViewState
["RepeatDirection"];
296 return (o
== null) ? RepeatDirection
.Vertical
: (RepeatDirection
) o
;
298 set { ViewState ["RepeatDirection"] = value; }
301 [DefaultValue (RepeatLayout
.Table
)]
302 [WebSysDescription ("")]
303 [WebCategory ("Layout")]
304 public virtual RepeatLayout RepeatLayout
{
306 object o
= ViewState
["RepeatLayout"];
307 return (o
== null) ? RepeatLayout
.Table
: (RepeatLayout
) o
;
310 if (value == RepeatLayout
.OrderedList
|| value == RepeatLayout
.UnorderedList
)
311 throw new ArgumentOutOfRangeException (String
.Format ("DataList does not support the '{0}' layout.", value));
312 ViewState
["RepeatLayout"] = value;
318 [WebSysDescription ("")]
319 [WebCategory ("Layout")]
320 public virtual int SelectedIndex
{
322 object o
= ViewState
["SelectedIndex"];
323 return (o
== null) ? -1 : (int) o
;
327 throw new ArgumentOutOfRangeException ("SelectedIndex", "< -1");
328 ViewState
["SelectedIndex"] = value;
333 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
334 [WebSysDescription ("")]
335 [WebCategory ("Layout")]
336 public virtual DataListItem SelectedItem
{
338 if (SelectedIndex
< 0)
340 if (SelectedIndex
>= Items
.Count
)
341 throw new ArgumentOutOfRangeException ("SelectedItem", ">= Items.Count");
342 return items
[SelectedIndex
];
346 [DefaultValue (null)]
347 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
348 [NotifyParentProperty (true)]
349 [PersistenceMode (PersistenceMode
.InnerProperty
)]
350 [WebSysDescription ("")]
351 [WebCategory ("Style")]
352 public virtual TableItemStyle SelectedItemStyle
{
354 if (selectedItemStyle
== null) {
355 selectedItemStyle
= new TableItemStyle ();
356 if (IsTrackingViewState
)
357 selectedItemStyle
.TrackViewState ();
359 return selectedItemStyle
;
364 [DefaultValue (null)]
365 [TemplateContainer (typeof (DataListItem
))]
366 [PersistenceMode (PersistenceMode
.InnerProperty
)]
367 [WebSysDescription ("")]
368 [WebCategory ("Style")]
369 public virtual ITemplate SelectedItemTemplate
{
370 get { return selectedItemTemplate; }
371 set { selectedItemTemplate = value; }
374 [DefaultValue (null)]
375 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
376 [NotifyParentProperty (true)]
377 [PersistenceMode (PersistenceMode
.InnerProperty
)]
378 [WebSysDescription ("")]
379 [WebCategory ("Style")]
380 public virtual TableItemStyle SeparatorStyle
{
382 if (separatorStyle
== null) {
383 separatorStyle
= new TableItemStyle ();
384 if (IsTrackingViewState
)
385 separatorStyle
.TrackViewState ();
387 return separatorStyle
;
392 [DefaultValue (null)]
393 [TemplateContainer (typeof (DataListItem
))]
394 [PersistenceMode (PersistenceMode
.InnerProperty
)]
395 [WebSysDescription ("")]
396 [WebCategory ("Style")]
397 public virtual ITemplate SeparatorTemplate
{
398 get { return separatorTemplate; }
399 set { separatorTemplate = value; }
402 [DefaultValue (true)]
403 [WebSysDescription ("")]
404 [WebCategory ("Appearance")]
405 public virtual bool ShowFooter
{
407 object o
= ViewState
["ShowFooter"];
408 return (o
== null) ? true : (bool) o
;
410 set { ViewState ["ShowFooter"] = value; }
413 [DefaultValue (true)]
414 [WebSysDescription ("")]
415 [WebCategory ("Appearance")]
416 public virtual bool ShowHeader
{
418 object o
= ViewState
["ShowHeader"];
419 return (o
== null) ? true : (bool) o
;
421 set { ViewState ["ShowHeader"] = value; }
424 [MonoTODO ("incomplete")]
426 public object SelectedValue
{
428 if (DataKeyField
.Length
== 0)
429 throw new InvalidOperationException (Locale
.GetText ("No DataKeyField present."));
431 int idx
= SelectedIndex
;
432 if ((idx
>= 0) && (idx
< DataKeys
.Count
))
433 return DataKeys
[idx
];
439 protected override HtmlTextWriterTag TagKey
{
440 get { return HtmlTextWriterTag.Table; }
443 TableStyle TableStyle
{
444 // this will throw an InvalidCasException just like we need
445 get { return (TableStyle) ControlStyle; }
451 list
= new ArrayList ();
456 void DoItem (int i
, ListItemType t
, object d
, bool databind
)
458 DataListItem itm
= CreateItem (i
, t
);
461 DataListItemEventArgs e
= new DataListItemEventArgs (itm
);
462 InitializeItem (itm
);
465 // It is very important that this be called *before* data
466 // binding. Otherwise, we won't save our state in the viewstate.
481 void DoItemInLoop (int i
, object d
, bool databind
, ListItemType type
)
483 DoItem (i
, type
, d
, databind
);
484 if (SeparatorTemplate
!= null)
485 DoItem (i
, ListItemType
.Separator
, null, databind
);
489 protected override void CreateControlHierarchy (bool useDataSource
)
494 IEnumerable ds
= null;
495 ArrayList keys
= null;
499 if (IsBoundUsingDataSourceID
)
502 ds
= DataSourceResolver
.ResolveDataSource (DataSource
, DataMember
);
503 keys
= DataKeysArray
;
506 idx
= (int) ViewState
["Items"];
508 if ((ds
== null) && (idx
== 0))
511 if (headerTemplate
!= null)
512 DoItem (-1, ListItemType
.Header
, null, useDataSource
);
515 int selected_index
= SelectedIndex
;
516 int edit_item_index
= EditItemIndex
;
519 string key
= DataKeyField
;
520 foreach (object o
in ds
) {
521 if (useDataSource
&& !String
.IsNullOrEmpty (key
))
522 keys
.Add (DataBinder
.GetPropertyValue (o
, key
));
523 type
= ListItemType
.Item
;
524 if (idx
== edit_item_index
)
525 type
= ListItemType
.EditItem
;
526 else if (idx
== selected_index
)
527 type
= ListItemType
.SelectedItem
;
528 else if ((idx
& 1) != 0)
529 type
= ListItemType
.AlternatingItem
;
531 DoItemInLoop (idx
, o
, useDataSource
, type
);
535 for (int i
= 0; i
< idx
; i
++) {
536 type
= ListItemType
.Item
;
537 if (i
== edit_item_index
)
538 type
= ListItemType
.EditItem
;
539 else if (i
== selected_index
)
540 type
= ListItemType
.SelectedItem
;
541 else if ((i
& 1) != 0)
542 type
= ListItemType
.AlternatingItem
;
544 DoItemInLoop (i
, null, useDataSource
, type
);
548 if (footerTemplate
!= null)
549 DoItem (-1, ListItemType
.Footer
, null, useDataSource
);
551 ViewState
["Items"] = idx
;
554 protected override Style
CreateControlStyle ()
556 // not kept (directly) in the DataList ViewState
557 TableStyle tableStyle
= new TableStyle ();
558 tableStyle
.CellSpacing
= 0;
562 protected virtual DataListItem
CreateItem (int itemIndex
, ListItemType itemType
)
564 return new DataListItem (itemIndex
, itemType
);
567 protected virtual void InitializeItem (DataListItem item
)
571 switch (item
.ItemType
) {
572 case ListItemType
.Header
:
575 case ListItemType
.Footer
:
578 case ListItemType
.Separator
:
579 t
= SeparatorTemplate
;
581 case ListItemType
.Item
:
582 case ListItemType
.AlternatingItem
:
583 case ListItemType
.SelectedItem
:
584 case ListItemType
.EditItem
:
585 if ((item
.ItemType
== ListItemType
.EditItem
) && (EditItemTemplate
!= null))
586 t
= EditItemTemplate
;
587 else if ((item
.ItemType
== ListItemType
.SelectedItem
) && (SelectedItemTemplate
!= null))
588 t
= SelectedItemTemplate
;
589 else if ((item
.ItemType
== ListItemType
.AlternatingItem
) && (AlternatingItemTemplate
!= null))
590 t
= AlternatingItemTemplate
;
597 t
.InstantiateIn (item
);
600 protected override void LoadViewState (object savedState
)
602 object[] state
= (object[]) savedState
;
603 base.LoadViewState (state
[0]);
604 if (state
[1] != null)
605 ItemStyle
.LoadViewState (state
[1]);
606 if (state
[2] != null)
607 SelectedItemStyle
.LoadViewState (state
[2]);
608 if (state
[3] != null)
609 AlternatingItemStyle
.LoadViewState (state
[3]);
610 if (state
[4] != null)
611 EditItemStyle
.LoadViewState (state
[4]);
612 if (state
[5] != null)
613 SeparatorStyle
.LoadViewState (state
[5]);
614 if (state
[6] != null)
615 HeaderStyle
.LoadViewState (state
[6]);
616 if (state
[7] != null)
617 FooterStyle
.LoadViewState (state
[7]);
618 if (state
[8] != null)
619 ControlStyle
.LoadViewState (state
[8]);
622 protected override bool OnBubbleEvent (object source
, EventArgs e
)
624 DataListCommandEventArgs dlca
= (e
as DataListCommandEventArgs
);
628 string cn
= dlca
.CommandName
;
629 CultureInfo inv
= Helpers
.InvariantCulture
;
631 OnItemCommand (dlca
);
632 if (String
.Compare (cn
, CancelCommandName
, true, inv
) == 0)
633 OnCancelCommand (dlca
);
634 else if (String
.Compare (cn
, DeleteCommandName
, true, inv
) == 0)
635 OnDeleteCommand (dlca
);
636 else if (String
.Compare (cn
, EditCommandName
, true, inv
) == 0)
637 OnEditCommand (dlca
);
638 else if (String
.Compare (cn
, SelectCommandName
, true, inv
) == 0) {
639 SelectedIndex
= dlca
.Item
.ItemIndex
;
640 OnSelectedIndexChanged (dlca
);
641 } else if (String
.Compare (cn
, UpdateCommandName
, true, inv
) == 0)
642 OnUpdateCommand (dlca
);
647 protected virtual void OnCancelCommand (DataListCommandEventArgs e
)
649 DataListCommandEventHandler cancelCommand
= (DataListCommandEventHandler
) Events
[cancelCommandEvent
];
650 if (cancelCommand
!= null)
651 cancelCommand (this, e
);
654 protected virtual void OnDeleteCommand (DataListCommandEventArgs e
)
656 DataListCommandEventHandler deleteCommand
= (DataListCommandEventHandler
) Events
[deleteCommandEvent
];
657 if (deleteCommand
!= null)
658 deleteCommand (this, e
);
661 protected virtual void OnEditCommand (DataListCommandEventArgs e
)
663 DataListCommandEventHandler editCommand
= (DataListCommandEventHandler
) Events
[editCommandEvent
];
664 if (editCommand
!= null)
665 editCommand (this, e
);
668 protected internal override void OnInit (EventArgs e
)
670 // EditItemIndex and SelectedIndex now use the Control State (i.e not the
674 page
.RegisterRequiresControlState (this);
678 protected virtual void OnItemCommand (DataListCommandEventArgs e
)
680 DataListCommandEventHandler itemCommand
= (DataListCommandEventHandler
) Events
[itemCommandEvent
];
681 if (itemCommand
!= null)
682 itemCommand (this, e
);
685 protected virtual void OnItemCreated (DataListItemEventArgs e
)
687 DataListItemEventHandler itemCreated
= (DataListItemEventHandler
) Events
[itemCreatedEvent
];
688 if (itemCreated
!= null)
689 itemCreated (this, e
);
692 protected virtual void OnItemDataBound (DataListItemEventArgs e
)
694 DataListItemEventHandler itemDataBound
= (DataListItemEventHandler
) Events
[itemDataBoundEvent
];
695 if (itemDataBound
!= null)
696 itemDataBound (this, e
);
699 protected virtual void OnUpdateCommand (DataListCommandEventArgs e
)
701 DataListCommandEventHandler updateCommand
= (DataListCommandEventHandler
) Events
[updateCommandEvent
];
702 if (updateCommand
!= null)
703 updateCommand (this, e
);
706 protected override void PrepareControlHierarchy ()
708 if (!HasControls () || Controls
.Count
== 0)
709 return; // No one called CreateControlHierarchy() with DataSource != null
712 foreach (DataListItem item
in Controls
) {
713 switch (item
.ItemType
) {
714 case ListItemType
.Item
:
715 item
.MergeStyle (itemStyle
);
717 case ListItemType
.AlternatingItem
:
719 if (alternatingItemStyle
!= null) {
720 alt
= new TableItemStyle ();
721 alt
.CopyFrom (itemStyle
);
722 alt
.CopyFrom (alternatingItemStyle
);
727 item
.MergeStyle (alt
);
729 case ListItemType
.EditItem
:
730 if (editItemStyle
!= null)
731 item
.MergeStyle (editItemStyle
);
733 item
.MergeStyle (itemStyle
);
735 case ListItemType
.Footer
:
737 item
.Visible
= false;
740 if (footerStyle
!= null)
741 item
.MergeStyle (footerStyle
);
743 case ListItemType
.Header
:
745 item
.Visible
= false;
748 if (headerStyle
!= null)
749 item
.MergeStyle (headerStyle
);
751 case ListItemType
.SelectedItem
:
752 if (selectedItemStyle
!= null)
753 item
.MergeStyle (selectedItemStyle
);
755 item
.MergeStyle (itemStyle
);
757 case ListItemType
.Separator
:
758 if (separatorStyle
!= null)
759 item
.MergeStyle(separatorStyle
);
761 item
.MergeStyle (itemStyle
);
767 protected internal override void RenderContents (HtmlTextWriter writer
)
769 if (Items
.Count
== 0)
772 RepeatInfo ri
= new RepeatInfo ();
773 ri
.RepeatColumns
= RepeatColumns
;
774 ri
.RepeatDirection
= RepeatDirection
;
775 ri
.RepeatLayout
= RepeatLayout
;
776 ri
.CaptionAlign
= CaptionAlign
;
777 ri
.Caption
= Caption
;
778 ri
.UseAccessibleHeader
= UseAccessibleHeader
;
780 // debugging stuff that I prefer to keep for a while
781 Console.WriteLine ("RepeatColumns {0}", ri.RepeatColumns);
782 Console.WriteLine ("RepeatDirection {0}", ri.RepeatDirection);
783 Console.WriteLine ("RepeatLayout {0}", ri.RepeatLayout);
784 Console.WriteLine ("OuterTableImplied {0}", ExtractTemplateRows);
785 Console.WriteLine ("IRepeatInfoUser.HasFooter {0}", (ShowFooter && (footerTemplate != null)));
786 Console.WriteLine ("IRepeatInfoUser.HasHeader {0}", (ShowHeader && (headerTemplate != null)));
787 Console.WriteLine ("IRepeatInfoUser.HasSeparators {0}", (separatorTemplate != null));
788 Console.WriteLine ("IRepeatInfoUser.RepeatedItemCount {0}", Items.Count);
789 for (int i=0; i < Items.Count; i++) {
790 DataListItem dli = Items [i];
791 Console.WriteLine ("{0}: Index {1}, Type {2}", i, dli.ItemIndex, dli.ItemType);
794 bool extract
= ExtractTemplateRows
;
796 ri
.OuterTableImplied
= true;
797 writer
.AddAttribute (HtmlTextWriterAttribute
.Id
, ClientID
);
798 if (ControlStyleCreated
)
799 ControlStyle
.AddAttributesToRender (writer
);
800 writer
.RenderBeginTag (HtmlTextWriterTag
.Table
);
801 ri
.RenderRepeater (writer
, this, ControlStyle
, this);
802 writer
.RenderEndTag ();
804 ri
.RenderRepeater (writer
, this, ControlStyle
, this);
807 protected override object SaveViewState ()
809 object[] state
= new object [9];
810 state
[0] = base.SaveViewState ();
811 if (itemStyle
!= null)
812 state
[1] = itemStyle
.SaveViewState ();
813 if (selectedItemStyle
!= null)
814 state
[2] = selectedItemStyle
.SaveViewState ();
815 if (alternatingItemStyle
!= null)
816 state
[3] = alternatingItemStyle
.SaveViewState ();
817 if (editItemStyle
!= null)
818 state
[4] = editItemStyle
.SaveViewState ();
819 if (separatorStyle
!= null)
820 state
[5] = separatorStyle
.SaveViewState ();
821 if (headerStyle
!= null)
822 state
[6] = headerStyle
.SaveViewState ();
823 if (footerStyle
!= null)
824 state
[7] = footerStyle
.SaveViewState ();
825 if (ControlStyleCreated
)
826 state
[8] = ControlStyle
.SaveViewState ();
830 protected override void TrackViewState ()
832 base.TrackViewState ();
833 if (alternatingItemStyle
!= null)
834 alternatingItemStyle
.TrackViewState ();
835 if (editItemStyle
!= null)
836 editItemStyle
.TrackViewState ();
837 if (footerStyle
!= null)
838 footerStyle
.TrackViewState ();
839 if (headerStyle
!= null)
840 headerStyle
.TrackViewState ();
841 if (itemStyle
!= null)
842 itemStyle
.TrackViewState ();
843 if (selectedItemStyle
!= null)
844 selectedItemStyle
.TrackViewState ();
845 if (separatorStyle
!= null)
846 separatorStyle
.TrackViewState ();
847 if (ControlStyleCreated
)
848 ControlStyle
.TrackViewState ();
852 [WebSysDescription ("")]
853 [WebCategory ("Action")]
854 public event DataListCommandEventHandler CancelCommand
{
855 add { Events.AddHandler (cancelCommandEvent, value); }
856 remove { Events.RemoveHandler (cancelCommandEvent, value); }
859 [WebSysDescription ("")]
860 [WebCategory ("Action")]
861 public event DataListCommandEventHandler DeleteCommand
{
862 add { Events.AddHandler (deleteCommandEvent, value); }
863 remove { Events.RemoveHandler (deleteCommandEvent, value); }
866 [WebSysDescription ("")]
867 [WebCategory ("Action")]
868 public event DataListCommandEventHandler EditCommand
{
869 add { Events.AddHandler (editCommandEvent, value); }
870 remove { Events.RemoveHandler (editCommandEvent, value); }
873 [WebSysDescription ("")]
874 [WebCategory ("Action")]
875 public event DataListCommandEventHandler ItemCommand
{
876 add { Events.AddHandler (itemCommandEvent, value); }
877 remove { Events.RemoveHandler (itemCommandEvent, value); }
880 [WebSysDescription ("")]
881 [WebCategory ("Action")]
882 public event DataListItemEventHandler ItemCreated
{
883 add { Events.AddHandler (itemCreatedEvent, value); }
884 remove { Events.RemoveHandler (itemCreatedEvent, value); }
887 [WebSysDescription ("")]
888 [WebCategory ("Action")]
889 public event DataListItemEventHandler ItemDataBound
{
890 add { Events.AddHandler (itemDataBoundEvent, value); }
891 remove { Events.RemoveHandler (itemDataBoundEvent, value); }
894 [WebSysDescription ("")]
895 [WebCategory ("Action")]
896 public event DataListCommandEventHandler UpdateCommand
{
897 add { Events.AddHandler (updateCommandEvent, value); }
898 remove { Events.RemoveHandler (updateCommandEvent, value); }
902 // IRepeatInfoUser support
904 bool IRepeatInfoUser
.HasFooter
{
905 get { return (ShowFooter && (footerTemplate != null)); }
908 bool IRepeatInfoUser
.HasHeader
{
909 get { return (ShowHeader && (headerTemplate != null)); }
912 bool IRepeatInfoUser
.HasSeparators
{
913 get { return (separatorTemplate != null); }
916 // don't include header, footer and separators in the count
917 int IRepeatInfoUser
.RepeatedItemCount
{
920 object o
= ViewState
["Items"];
921 idx
= (o
== null) ? 0 : (int) o
;
927 Style IRepeatInfoUser
.GetItemStyle (ListItemType itemType
, int repeatIndex
)
929 DataListItem item
= null;
931 case ListItemType
.Header
:
932 case ListItemType
.Footer
:
933 if (repeatIndex
>= 0 && (!HasControls () || repeatIndex
>= Controls
.Count
))
934 throw new ArgumentOutOfRangeException ();
936 item
= FindFirstItem (itemType
);
938 case ListItemType
.Item
:
939 case ListItemType
.AlternatingItem
:
940 case ListItemType
.SelectedItem
:
941 case ListItemType
.EditItem
:
942 if (repeatIndex
>= 0 && (!HasControls () || repeatIndex
>= Controls
.Count
))
943 throw new ArgumentOutOfRangeException ();
945 item
= FindBestItem (repeatIndex
);
947 case ListItemType
.Separator
:
948 if (repeatIndex
>= 0 && (!HasControls () || repeatIndex
>= Controls
.Count
))
949 throw new ArgumentOutOfRangeException ();
951 item
= FindSpecificItem (itemType
, repeatIndex
);
958 if (item
== null || item
.ControlStyleCreated
== false)
961 return item
.ControlStyle
;
964 // Header and Footer don't have a "real" index (-1)
965 DataListItem
FindFirstItem (ListItemType itemType
)
967 for (int i
= 0; i
< Controls
.Count
; i
++) {
968 DataListItem item
= (Controls
[i
] as DataListItem
);
969 if ((item
!= null) && (item
.ItemType
== itemType
))
975 // Both Type and Index must match (e.g. Separator)
976 DataListItem
FindSpecificItem (ListItemType itemType
, int repeatIndex
)
978 for (int i
= 0; i
< Controls
.Count
; i
++) {
979 DataListItem item
= (Controls
[i
] as DataListItem
);
980 if ((item
!= null) && (item
.ItemType
== itemType
) && (item
.ItemIndex
== repeatIndex
))
986 // we get call for Item even for AlternatingItem :(
987 DataListItem
FindBestItem (int repeatIndex
)
989 for (int i
= 0; i
< Controls
.Count
; i
++) {
990 DataListItem item
= (Controls
[i
] as DataListItem
);
991 if ((item
!= null) && (item
.ItemIndex
== repeatIndex
)) {
992 switch (item
.ItemType
) {
993 case ListItemType
.Item
:
994 case ListItemType
.AlternatingItem
:
995 case ListItemType
.SelectedItem
:
996 case ListItemType
.EditItem
:
1006 void IRepeatInfoUser
.RenderItem (ListItemType itemType
, int repeatIndex
, RepeatInfo repeatInfo
, HtmlTextWriter writer
)
1008 // if possible take the easy way out...
1009 if (!HasControls ())
1012 DataListItem item
= null;
1014 case ListItemType
.Header
:
1015 case ListItemType
.Footer
:
1016 item
= FindFirstItem (itemType
);
1018 case ListItemType
.Item
:
1019 case ListItemType
.AlternatingItem
:
1020 case ListItemType
.SelectedItem
:
1021 case ListItemType
.EditItem
:
1022 item
= FindBestItem (repeatIndex
);
1024 case ListItemType
.Separator
:
1025 item
= FindSpecificItem (itemType
, repeatIndex
);
1030 bool extract
= ExtractTemplateRows
;
1031 bool table
= (RepeatLayout
== RepeatLayout
.Table
);
1032 if (!table
|| extract
) {
1033 // sadly RepeatInfo doesn't support Style for RepeatLayout.Flow
1034 Style s
= (this as IRepeatInfoUser
).GetItemStyle (itemType
, repeatIndex
);
1036 item
.ControlStyle
.CopyFrom (s
);
1038 //Console.WriteLine ("RenderItem #{0} type {1}", repeatIndex, itemType);
1039 item
.RenderItem (writer
, extract
, table
);
1041 //Console.WriteLine ("Couldn't find #{0} type {1} out of {2} items / {3} controls", repeatIndex, itemType, Items.Count, Controls.Count);