| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- //
- // System.Web.UI.WebControls.DataList.cs
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Collections;
- using System.ComponentModel;
- using System.Globalization;
- using System.Security.Permissions;
- using System.Web.Util;
- namespace System.Web.UI.WebControls {
- // CAS
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- // attributes
- [Designer ("System.Web.UI.Design.WebControls.DataListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
- #if NET_2_0
- [ControlValueProperty ("SelectedValue")]
- [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, "System.ComponentModel.ComponentEditor, " + Consts.AssemblySystem)]
- #else
- [Editor ("System.Web.UI.Design.WebControls.DataListComponentEditor, " + Consts.AssemblySystem_Design, typeof (System.ComponentModel.ComponentEditor))]
- #endif
- public class DataList : BaseDataList, INamingContainer, IRepeatInfoUser {
- public const string CancelCommandName = "Cancel";
- public const string DeleteCommandName = "Delete";
- public const string EditCommandName = "Edit";
- public const string SelectCommandName = "Select";
- public const string UpdateCommandName = "Update";
- private static readonly object cancelCommandEvent = new object ();
- private static readonly object deleteCommandEvent = new object ();
- private static readonly object editCommandEvent = new object ();
- private static readonly object itemCommandEvent = new object ();
- private static readonly object itemCreatedEvent = new object ();
- private static readonly object itemDataBoundEvent = new object ();
- private static readonly object updateCommandEvent = new object ();
- private TableItemStyle alternatingItemStyle;
- private TableItemStyle editItemStyle;
- private TableItemStyle footerStyle;
- private TableItemStyle headerStyle;
- private TableItemStyle itemStyle;
- private TableItemStyle selectedItemStyle;
- private TableItemStyle separatorStyle;
- private ITemplate alternatingItemTemplate;
- private ITemplate editItemTemplate;
- private ITemplate footerTemplate;
- private ITemplate headerTemplate;
- private ITemplate itemTemplate;
- private ITemplate selectedItemTemplate;
- private ITemplate separatorTemplate;
- private DataListItemCollection items;
- private ArrayList list;
- private int idx;
- #if NET_2_0
- private int editIndex;
- private int selectedIndex;
- public DataList ()
- {
- editIndex = -1;
- selectedIndex = -1;
- idx = -1;
- }
- #else
- public DataList ()
- {
- idx = -1;
- }
- #endif
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle AlternatingItemStyle {
- get {
- if (alternatingItemStyle == null) {
- alternatingItemStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- alternatingItemStyle.TrackViewState ();
- }
- }
- return alternatingItemStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate AlternatingItemTemplate {
- get { return alternatingItemTemplate; }
- set { alternatingItemTemplate = value; }
- }
- [DefaultValue (-1)]
- [WebSysDescription ("")]
- [WebCategory ("Misc")]
- public virtual int EditItemIndex {
- get {
- object o = ViewState ["EditItemIndex"];
- return (o == null) ? -1 : (int) o;
- }
- set {
- if (value < -1)
- throw new ArgumentOutOfRangeException ("EditItemIndex", "< -1");
- ViewState ["EditItemIndex"] = value;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle EditItemStyle {
- get {
- if (editItemStyle == null) {
- editItemStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- editItemStyle.TrackViewState ();
- }
- }
- return editItemStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate EditItemTemplate {
- get { return editItemTemplate; }
- set { editItemTemplate = value; }
- }
- [DefaultValue (false)]
- [WebSysDescription ("")]
- [WebCategory ("Misc")]
- public virtual bool ExtractTemplateRows {
- get {
- object o = ViewState ["ExtractTemplateRows"];
- return (o == null) ? false : (bool) o;
- }
- set { ViewState ["ExtractTemplateRows"] = value; }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle FooterStyle {
- get {
- if (footerStyle == null) {
- footerStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- footerStyle.TrackViewState ();
- }
- }
- return footerStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate FooterTemplate {
- get { return footerTemplate; }
- set { footerTemplate = value; }
- }
- // yes! they do NOT match in fx1.1
- [DefaultValue (GridLines.None)]
- public override GridLines GridLines {
- get {
- if (!ControlStyleCreated) {
- #if NET_2_0
- return GridLines.None;
- #else
- return GridLines.Both;
- #endif
- }
- return TableStyle.GridLines;
- }
- set { TableStyle.GridLines = value; }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle HeaderStyle {
- get {
- if (headerStyle == null) {
- headerStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- headerStyle.TrackViewState ();
- }
- }
- return headerStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate HeaderTemplate {
- get { return headerTemplate; }
- set { headerTemplate = value; }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual DataListItemCollection Items {
- get {
- if (items == null)
- items = new DataListItemCollection (ItemList);
- return items;
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle ItemStyle {
- get {
- if (itemStyle == null) {
- itemStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- itemStyle.TrackViewState ();
- }
- }
- return itemStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate ItemTemplate {
- get { return itemTemplate; }
- set { itemTemplate = value; }
- }
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [DefaultValue (0)]
- [WebSysDescription ("")]
- [WebCategory ("Layout")]
- public virtual int RepeatColumns {
- get {
- object o = ViewState ["RepeatColumns"];
- return (o == null) ? 0 : (int) o;
- }
- set {
- if (value < 0)
- throw new ArgumentOutOfRangeException ("value", "RepeatColumns value has to be 0 for 'not set' or > 0.");
-
- ViewState ["RepeatColumns"] = value;
- }
- }
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [DefaultValue (RepeatDirection.Vertical)]
- [WebSysDescription ("")]
- [WebCategory ("Layout")]
- public virtual RepeatDirection RepeatDirection {
- get {
- object o = ViewState ["RepeatDirection"];
- return (o == null) ? RepeatDirection.Vertical : (RepeatDirection) o;
- }
- set { ViewState ["RepeatDirection"] = value; }
- }
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [DefaultValue (RepeatLayout.Table)]
- [WebSysDescription ("")]
- [WebCategory ("Layout")]
- public virtual RepeatLayout RepeatLayout {
- get {
- object o = ViewState ["RepeatLayout"];
- return (o == null) ? RepeatLayout.Table : (RepeatLayout) o;
- }
- set { ViewState ["RepeatLayout"] = value; }
- }
- [Bindable (true)]
- [DefaultValue (-1)]
- [WebSysDescription ("")]
- [WebCategory ("Layout")]
- public virtual int SelectedIndex {
- get {
- object o = ViewState ["SelectedIndex"];
- return (o == null) ? -1 : (int) o;
- }
- set {
- if (value < -1)
- throw new ArgumentOutOfRangeException ("SelectedIndex", "< -1");
- ViewState ["SelectedIndex"] = value;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("")]
- [WebCategory ("Layout")]
- public virtual DataListItem SelectedItem {
- get {
- if (SelectedIndex < 0)
- return null;
- if (SelectedIndex >= Items.Count)
- throw new ArgumentOutOfRangeException ("SelectedItem", ">= Items.Count");
- return items [SelectedIndex];
- }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle SelectedItemStyle {
- get {
- if (selectedItemStyle == null) {
- selectedItemStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- selectedItemStyle.TrackViewState ();
- }
- }
- return selectedItemStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate SelectedItemTemplate {
- get { return selectedItemTemplate; }
- set { selectedItemTemplate = value; }
- }
- [DefaultValue (null)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [NotifyParentProperty (true)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual TableItemStyle SeparatorStyle {
- get {
- if (separatorStyle == null) {
- separatorStyle = new TableItemStyle ();
- if (IsTrackingViewState) {
- separatorStyle.TrackViewState ();
- }
- }
- return separatorStyle;
- }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [TemplateContainer (typeof (DataListItem))]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [WebSysDescription ("")]
- [WebCategory ("Style")]
- public virtual ITemplate SeparatorTemplate {
- get { return separatorTemplate; }
- set { separatorTemplate = value; }
- }
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [DefaultValue (true)]
- [WebSysDescription ("")]
- [WebCategory ("Appearance")]
- public virtual bool ShowFooter {
- get {
- object o = ViewState ["ShowFooter"];
- return (o == null) ? true : (bool) o;
- }
- set { ViewState ["ShowFooter"] = value; }
- }
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [DefaultValue (true)]
- [WebSysDescription ("")]
- [WebCategory ("Appearance")]
- public virtual bool ShowHeader {
- get {
- object o = ViewState ["ShowHeader"];
- return (o == null) ? true : (bool) o;
- }
- set { ViewState ["ShowHeader"] = value; }
- }
- #if NET_2_0
- [MonoTODO ("incomplete")]
- [Browsable (false)]
- public object SelectedValue {
- get {
- if (DataKeyField.Length == 0)
- throw new InvalidOperationException (Locale.GetText ("No DataKeyField present."));
- if ((SelectedIndex >= 0) && (selectedIndex < DataKeys.Count)) {
- return DataKeys [selectedIndex];
- }
- return null;
- }
- }
- protected override HtmlTextWriterTag TagKey {
- get { return HtmlTextWriterTag.Table; }
- }
- #endif
- private TableStyle TableStyle {
- // this will throw an InvalidCasException just like we need
- get { return (TableStyle) ControlStyle; }
- }
-
- private ArrayList ItemList {
- get {
- if (list == null)
- list = new ArrayList ();
- return list;
- }
- }
- void DoItem (int i, ListItemType t, object d, bool databind)
- {
- DataListItem itm = CreateItem (i, t);
- if (databind)
- itm.DataItem = d;
- DataListItemEventArgs e = new DataListItemEventArgs (itm);
- InitializeItem (itm);
-
- //
- // It is very important that this be called *before* data
- // binding. Otherwise, we won't save our state in the viewstate.
- //
- Controls.Add (itm);
- if (i != -1)
- ItemList.Add (itm);
- OnItemCreated (e);
- if (databind) {
- itm.DataBind ();
- OnItemDataBound (e);
- itm.DataItem = null;
- }
- }
- private void DoItemInLoop (int i, object d, bool databind, ListItemType type)
- {
- DoItem (i, type, d, databind);
- if (SeparatorTemplate != null)
- DoItem (i, ListItemType.Separator, null, databind);
- }
- protected override void CreateControlHierarchy (bool useDataSource)
- {
- Controls.Clear();
- IEnumerable ds = null;
- ArrayList keys = null;
- if (useDataSource) {
- idx = 0;
- ds = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
- keys = DataKeysArray;
- keys.Clear ();
- } else {
- idx = (int) ViewState ["Items"];
- }
- if ((ds == null) && (idx == 0))
- return;
- if (headerTemplate != null)
- DoItem (-1, ListItemType.Header, null, useDataSource);
- // items
- int selected_index = SelectedIndex;
- int edit_item_index = EditItemIndex;
- ListItemType type;
- if (ds != null) {
- string key = DataKeyField;
- foreach (object o in ds) {
- if (useDataSource && key != "")
- keys.Add (DataBinder.GetPropertyValue (o, key));
- type = ListItemType.Item;
- if (idx == edit_item_index)
- type = ListItemType.EditItem;
- else if (idx == selected_index)
- type = ListItemType.SelectedItem;
- else if (idx % 2 != 0)
- type = ListItemType.AlternatingItem;
- DoItemInLoop (idx, o, useDataSource, type);
- idx++;
- }
- } else {
- for (int i = 0; i < idx; i++) {
- type = ListItemType.Item;
- if (idx == edit_item_index)
- type = ListItemType.EditItem;
- else if (idx == selected_index)
- type = ListItemType.SelectedItem;
- else if (idx % 2 != 0)
- type = ListItemType.AlternatingItem;
- DoItemInLoop (i, null, useDataSource, type);
- }
- }
- if (footerTemplate != null)
- DoItem (-1, ListItemType.Footer, null, useDataSource);
- ViewState ["Items"] = idx;
- }
- protected override Style CreateControlStyle ()
- {
- #if NET_2_0
- // not kept (directly) in the DataList ViewState
- return new TableStyle ();
- #else
- TableStyle tableStyle = new TableStyle (ViewState);
- tableStyle.CellSpacing = 0;
- return tableStyle;
- #endif
- }
- protected virtual DataListItem CreateItem (int itemIndex, ListItemType itemType)
- {
- return new DataListItem (itemIndex, itemType);
- }
- protected virtual void InitializeItem (DataListItem item)
- {
- ITemplate t = null;
-
- switch (item.ItemType) {
- case ListItemType.Header:
- t = HeaderTemplate;
- break;
- case ListItemType.Footer:
- t = FooterTemplate;
- break;
- case ListItemType.Separator:
- t = SeparatorTemplate;
- break;
- case ListItemType.Item:
- case ListItemType.AlternatingItem:
- case ListItemType.SelectedItem:
- case ListItemType.EditItem:
- int index = item.ItemIndex;
- if ((SelectedIndex == index) && (SelectedItemTemplate != null))
- t = SelectedItemTemplate;
- else if ((EditItemIndex == index) && (EditItemTemplate != null))
- t = EditItemTemplate;
- else if (((index & 1) == 0) || (alternatingItemTemplate == null))
- t = ItemTemplate;
- else
- t = alternatingItemTemplate;
- break;
- }
- if (t != null)
- t.InstantiateIn (item);
- }
- protected override void LoadViewState (object savedState)
- {
- object[] state = (object[]) savedState;
- base.LoadViewState (state [0]);
- if (state [1] != null)
- ItemStyle.LoadViewState (state [1]);
- if (state [2] != null)
- SelectedItemStyle.LoadViewState (state [2]);
- if (state [3] != null)
- AlternatingItemStyle.LoadViewState (state [3]);
- if (state [4] != null)
- EditItemStyle.LoadViewState (state [4]);
- if (state [5] != null)
- SeparatorStyle.LoadViewState (state [5]);
- if (state [6] != null)
- HeaderStyle.LoadViewState (state [6]);
- if (state [7] != null)
- FooterStyle.LoadViewState (state [7]);
- }
- protected override bool OnBubbleEvent (object source, EventArgs e)
- {
- DataListCommandEventArgs dlca = (e as DataListCommandEventArgs);
- if (dlca == null)
- return false;
- string cn = dlca.CommandName;
- CultureInfo inv = CultureInfo.InvariantCulture;
- OnItemCommand (dlca);
- if (String.Compare (cn, CancelCommandName, true, inv) == 0) {
- OnCancelCommand (dlca);
- return true;
- } else if (String.Compare (cn, DeleteCommandName, true, inv) == 0) {
- OnDeleteCommand (dlca);
- return true;
- } else if (String.Compare (cn, EditCommandName, true, inv) == 0) {
- OnEditCommand (dlca);
- return true;
- } else if (String.Compare (cn, SelectCommandName, true, inv) == 0) {
- OnSelectedIndexChanged (dlca);
- return true;
- } else if (String.Compare (cn, UpdateCommandName, true, inv) == 0) {
- OnUpdateCommand (dlca);
- return true;
- }
- return false;
- }
- protected virtual void OnCancelCommand (DataListCommandEventArgs e)
- {
- DataListCommandEventHandler cancelCommand = (DataListCommandEventHandler) Events [cancelCommandEvent];
- if (cancelCommand != null)
- cancelCommand (this, e);
- }
- protected virtual void OnDeleteCommand (DataListCommandEventArgs e)
- {
- DataListCommandEventHandler deleteCommand = (DataListCommandEventHandler) Events [deleteCommandEvent];
- if (deleteCommand != null)
- deleteCommand (this, e);
- }
- protected virtual void OnEditCommand (DataListCommandEventArgs e)
- {
- DataListCommandEventHandler editCommand = (DataListCommandEventHandler) Events [editCommandEvent];
- if (editCommand != null)
- editCommand (this, e);
- }
- #if NET_2_0
- protected internal override void OnInit (EventArgs e)
- {
- // EditItemIndex and SelectedIndex now use the Control State (i.e not the View State)
- Page.RegisterRequiresControlState (this);
- base.OnInit (e);
- }
- #endif
- protected virtual void OnItemCommand (DataListCommandEventArgs e)
- {
- DataListCommandEventHandler itemCommand = (DataListCommandEventHandler) Events [itemCommandEvent];
- if (itemCommand != null)
- itemCommand (this, e);
- }
- protected virtual void OnItemCreated (DataListItemEventArgs e)
- {
- DataListItemEventHandler itemCreated = (DataListItemEventHandler) Events [itemCreatedEvent];
- if (itemCreated != null)
- itemCreated (this, e);
- }
- protected virtual void OnItemDataBound (DataListItemEventArgs e)
- {
- DataListItemEventHandler itemDataBound = (DataListItemEventHandler) Events [itemDataBoundEvent];
- if (itemDataBound != null)
- itemDataBound (this, e);
- }
- protected virtual void OnUpdateCommand (DataListCommandEventArgs e)
- {
- DataListCommandEventHandler updateCommand = (DataListCommandEventHandler) Events [updateCommandEvent];
- if (updateCommand != null)
- updateCommand (this, e);
- }
- void ApplyControlStyle (Control ctrl, Style style)
- {
- if (style == null || false == ctrl.HasControls ())
- return;
- foreach (Control c in ctrl.Controls) {
- if (c is Table) {
- Table tbl = (Table) c;
- foreach (TableRow row in tbl.Rows)
- row.MergeStyle (style);
- }
- }
- }
- protected override void PrepareControlHierarchy ()
- {
- if (!HasControls () || Controls.Count == 0)
- return; // No one called CreateControlHierarchy() with DataSource != null
- Style alt = null;
- foreach (DataListItem item in Controls) {
- switch (item.ItemType) {
- case ListItemType.Item:
- item.MergeStyle (itemStyle);
- ApplyControlStyle (item, itemStyle);
- break;
- case ListItemType.AlternatingItem:
- if (alt == null) {
- if (alternatingItemStyle != null) {
- alt = new TableItemStyle ();
- alt.CopyFrom (itemStyle);
- alt.CopyFrom (alternatingItemStyle);
- } else {
- alt = itemStyle;
- }
- }
- item.MergeStyle (alt);
- ApplyControlStyle (item, alt);
- break;
- case ListItemType.EditItem:
- if (editItemStyle != null) {
- item.MergeStyle (editItemStyle);
- ApplyControlStyle (item, editItemStyle);
- } else {
- item.MergeStyle (itemStyle);
- ApplyControlStyle (item, itemStyle);
- }
- break;
- case ListItemType.Footer:
- if (!ShowFooter) {
- item.Visible = false;
- break;
- }
- if (footerStyle != null) {
- item.MergeStyle (footerStyle);
- ApplyControlStyle (item, footerStyle);
- }
- break;
- case ListItemType.Header:
- if (!ShowHeader) {
- item.Visible = false;
- break;
- }
- if (headerStyle != null) {
- item.MergeStyle (headerStyle);
- ApplyControlStyle (item, headerStyle);
- }
- break;
- case ListItemType.SelectedItem:
- if (selectedItemStyle != null) {
- item.MergeStyle (selectedItemStyle);
- ApplyControlStyle (item, selectedItemStyle);
- } else {
- item.MergeStyle (itemStyle);
- ApplyControlStyle (item, itemStyle);
- }
- break;
- case ListItemType.Separator:
- if (separatorStyle != null) {
- item.MergeStyle(separatorStyle);
- ApplyControlStyle (item, separatorStyle);
- }
- else {
- item.MergeStyle (itemStyle);
- ApplyControlStyle (item, itemStyle);
- }
- break;
- }
- }
- }
- #if NET_2_0
- protected internal
- #else
- protected
- #endif
- override void RenderContents (HtmlTextWriter writer)
- {
- if (Items.Count == 0)
- return;
- RepeatInfo ri = new RepeatInfo ();
- ri.RepeatColumns = RepeatColumns;
- ri.RepeatDirection = RepeatDirection;
- ri.RepeatLayout = RepeatLayout;
- ri.CaptionAlign = CaptionAlign;
- ri.Caption = Caption;
- ri.UseAccessibleHeader = UseAccessibleHeader;
- /*
- // debugging stuff that I prefer to keep for a while
- Console.WriteLine ("RepeatColumns {0}", ri.RepeatColumns);
- Console.WriteLine ("RepeatDirection {0}", ri.RepeatDirection);
- Console.WriteLine ("RepeatLayout {0}", ri.RepeatLayout);
- Console.WriteLine ("OuterTableImplied {0}", ExtractTemplateRows);
- Console.WriteLine ("IRepeatInfoUser.HasFooter {0}", (ShowFooter && (footerTemplate != null)));
- Console.WriteLine ("IRepeatInfoUser.HasHeader {0}", (ShowHeader && (headerTemplate != null)));
- Console.WriteLine ("IRepeatInfoUser.HasSeparators {0}", (separatorTemplate != null));
- Console.WriteLine ("IRepeatInfoUser.RepeatedItemCount {0}", Items.Count);
- for (int i=0; i < Items.Count; i++) {
- DataListItem dli = Items [i];
- Console.WriteLine ("{0}: Index {1}, Type {2}", i, dli.ItemIndex, dli.ItemType);
- }
- */
- bool extract = ExtractTemplateRows;
- if (extract) {
- ri.OuterTableImplied = true;
- writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
- if (ControlStyleCreated) {
- ControlStyle.AddAttributesToRender (writer);
- }
- writer.RenderBeginTag (HtmlTextWriterTag.Table);
- ri.RenderRepeater (writer, this, ControlStyle, this);
- writer.RenderEndTag ();
- } else {
- ri.RenderRepeater (writer, this, ControlStyle, this);
- }
- }
- protected override object SaveViewState ()
- {
- #if NET_2_0
- object[] state = new object [9];
- // FIXME: what's new in slot #0 ?
- state[8] = base.SaveViewState ();
- #else
- object[] state = new object [8];
- state[0] = base.SaveViewState ();
- #endif
- if (itemStyle != null)
- state [1] = itemStyle.SaveViewState ();
- if (selectedItemStyle != null)
- state [2] = selectedItemStyle.SaveViewState ();
- if (alternatingItemStyle != null)
- state [3] = alternatingItemStyle.SaveViewState ();
- if (editItemStyle != null)
- state [4] = editItemStyle.SaveViewState ();
- if (separatorStyle != null)
- state [5] = separatorStyle.SaveViewState ();
- if (headerStyle != null)
- state [6] = headerStyle.SaveViewState ();
- if (footerStyle != null)
- state [7] = footerStyle.SaveViewState ();
- return state;
- }
- protected override void TrackViewState ()
- {
- base.TrackViewState ();
- if (alternatingItemStyle != null)
- alternatingItemStyle.TrackViewState ();
- if (editItemStyle != null)
- editItemStyle.TrackViewState ();
- if (footerStyle != null)
- footerStyle.TrackViewState ();
- if (headerStyle != null)
- headerStyle.TrackViewState ();
- if (itemStyle != null)
- itemStyle.TrackViewState ();
- if (selectedItemStyle != null)
- selectedItemStyle.TrackViewState ();
- if (separatorStyle != null)
- separatorStyle.TrackViewState ();
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListCommandEventHandler CancelCommand {
- add { Events.AddHandler (cancelCommandEvent, value); }
- remove { Events.RemoveHandler (cancelCommandEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListCommandEventHandler DeleteCommand {
- add { Events.AddHandler (deleteCommandEvent, value); }
- remove { Events.RemoveHandler (deleteCommandEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListCommandEventHandler EditCommand {
- add { Events.AddHandler (editCommandEvent, value); }
- remove { Events.RemoveHandler (editCommandEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListCommandEventHandler ItemCommand {
- add { Events.AddHandler (itemCommandEvent, value); }
- remove { Events.RemoveHandler (itemCommandEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListItemEventHandler ItemCreated {
- add { Events.AddHandler (itemCreatedEvent, value); }
- remove { Events.RemoveHandler (itemCreatedEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListItemEventHandler ItemDataBound {
- add { Events.AddHandler (itemDataBoundEvent, value); }
- remove { Events.RemoveHandler (itemDataBoundEvent, value); }
- }
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event DataListCommandEventHandler UpdateCommand {
- add { Events.AddHandler (updateCommandEvent, value); }
- remove { Events.RemoveHandler (updateCommandEvent, value); }
- }
- // IRepeatInfoUser support
- bool IRepeatInfoUser.HasFooter {
- get { return (ShowFooter && (footerTemplate != null)); }
- }
- bool IRepeatInfoUser.HasHeader {
- get { return (ShowHeader && (headerTemplate != null)); }
- }
-
- bool IRepeatInfoUser.HasSeparators {
- get { return (separatorTemplate != null); }
- }
- // don't include header, footer and separators in the count
- int IRepeatInfoUser.RepeatedItemCount {
- get {
- if (idx == -1) {
- object o = ViewState ["Items"];
- idx = (o == null) ? 0 : (int) o;
- }
- return idx;
- }
- }
- Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
- {
- DataListItem item = null;
- switch (itemType) {
- case ListItemType.Header:
- case ListItemType.Footer:
- if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
- throw new ArgumentOutOfRangeException ();
- item = FindFirstItem (itemType);
- break;
- case ListItemType.Item:
- case ListItemType.AlternatingItem:
- case ListItemType.SelectedItem:
- case ListItemType.EditItem:
- if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
- throw new ArgumentOutOfRangeException ();
- item = FindBestItem (repeatIndex);
- break;
- case ListItemType.Separator:
- if (repeatIndex >= 0 && (!HasControls () || repeatIndex >= Controls.Count))
- throw new ArgumentOutOfRangeException ();
- item = FindSpecificItem (itemType, repeatIndex);
- break;
- default:
- item = null;
- break;
- }
- if (item == null || item.ControlStyleCreated == false)
- return null;
- return item.ControlStyle;
- }
- // Header and Footer don't have a "real" index (-1)
- private DataListItem FindFirstItem (ListItemType itemType)
- {
- for (int i = 0; i < Controls.Count; i++) {
- DataListItem item = (Controls [i] as DataListItem);
- if ((item != null) && (item.ItemType == itemType))
- return item;
- }
- return null;
- }
- // Both Type and Index must match (e.g. Separator)
- private DataListItem FindSpecificItem (ListItemType itemType, int repeatIndex)
- {
- for (int i = 0; i < Controls.Count; i++) {
- DataListItem item = (Controls [i] as DataListItem);
- if ((item != null) && (item.ItemType == itemType) && (item.ItemIndex == repeatIndex))
- return item;
- }
- return null;
- }
- // we get call for Item even for AlternatingItem :(
- private DataListItem FindBestItem (int repeatIndex)
- {
- for (int i = 0; i < Controls.Count; i++) {
- DataListItem item = (Controls [i] as DataListItem);
- if ((item != null) && (item.ItemIndex == repeatIndex)) {
- switch (item.ItemType) {
- case ListItemType.Item:
- case ListItemType.AlternatingItem:
- case ListItemType.SelectedItem:
- case ListItemType.EditItem:
- return item;
- default:
- return null;
- }
- }
- }
- return null;
- }
- void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
- {
- // if possible take the easy way out...
- if (!HasControls ())
- return;
- DataListItem item = null;
- switch (itemType) {
- case ListItemType.Header:
- case ListItemType.Footer:
- item = FindFirstItem (itemType);
- break;
- case ListItemType.Item:
- case ListItemType.AlternatingItem:
- case ListItemType.SelectedItem:
- case ListItemType.EditItem:
- item = FindBestItem (repeatIndex);
- break;
- case ListItemType.Separator:
- item = FindSpecificItem (itemType, repeatIndex);
- break;
- }
- if (item != null) {
- bool extract = ExtractTemplateRows;
- bool table = (RepeatLayout == RepeatLayout.Table);
- if (!table || extract) {
- // sadly RepeatInfo doesn't support Style for RepeatLayout.Flow
- Style s = (this as IRepeatInfoUser).GetItemStyle (itemType, repeatIndex);
- if (s != null)
- item.ControlStyle.CopyFrom (s);
- }
- //Console.WriteLine ("RenderItem #{0} type {1}", repeatIndex, itemType);
- item.RenderItem (writer, extract, table);
- } else {
- //Console.WriteLine ("Couldn't find #{0} type {1} out of {2} items / {3} controls", repeatIndex, itemType, Items.Count, Controls.Count);
- }
- }
- }
- }
|