Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListItemCollection.cs
index d6c2ed89790bac42aad81ecb5349c27020775744..b0d19ca082776497df4fe166e025b491ee4cc6b8 100644 (file)
@@ -36,20 +36,22 @@ namespace System.Web.UI.WebControls {
        [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        // attributes
        [Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
-       public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager {
-               #region Fields
-               private ArrayList       items;
-               private bool            tracking;
-               private bool            dirty;
-               #endregion      // Fields
-
-               #region Public Constructors
+       public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager 
+       {
+#region Fields
+               ArrayList items;
+               bool tracking;
+               bool dirty;
+               int lastDirty = 0;
+#endregion     // Fields
+
+#region Public Constructors
                public ListItemCollection() {
                        items = new ArrayList();
                }
-               #endregion      // Public Constructors
+#endregion     // Public Constructors
 
-               #region Public Instance Properties
+#region Public Instance Properties
                public int Capacity {
                        get {
                                return items.Capacity;
@@ -174,6 +176,7 @@ namespace System.Web.UI.WebControls {
 
                        if (tracking) {
                                item.TrackViewState ();
+                               lastDirty = index;
                                SetDirty ();
                        }
                }
@@ -184,6 +187,7 @@ namespace System.Web.UI.WebControls {
 
                        if (tracking) {
                                listItem.TrackViewState ();
+                               lastDirty = index;
                                SetDirty ();
                        }
                }
@@ -212,9 +216,9 @@ namespace System.Web.UI.WebControls {
                        if (tracking)
                                SetDirty ();
                }
-               #endregion      // Public Instance Methods
+#endregion     // Public Instance Methods
 
-               #region Interface methods
+#region Interface methods
                bool IList.IsFixedSize {
                        get {
                                return items.IsFixedSize;
@@ -268,42 +272,62 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               void IStateManager.LoadViewState(object state) {
-
-                       if (state == null)
+               void IStateManager.LoadViewState (object savedState)
+               {
+                       Pair pair = savedState as Pair;
+                       if (pair == null)
                                return;
 
-                       object [] stateObj = (object[]) state;
-                       int count = stateObj.Length;
+                       bool newCollection = (bool) pair.First;
+                       object [] itemsArray = (object []) pair.Second;
+                       int count = itemsArray==null ? 0 : itemsArray.Length;
 
-                       items = new ArrayList(count);
+                       if (newCollection)
+                               if (count > 0)
+                                       items = new ArrayList(count);
+                               else
+                                       items = new ArrayList();
 
                        for (int i = 0; i < count; i++) {
                                ListItem item = new ListItem ();
-                               if (stateObj [i] != null)
-                                       item.LoadViewState (stateObj [i]);
-
-                               items.Add (item);
+                               
+                               if (newCollection) {
+                                       item.LoadViewState (itemsArray [i]);
+                                       item.SetDirty ();
+                                       Add (item);
+                               }
+                               else{
+                                       if (itemsArray [i] != null){
+                                               item.LoadViewState (itemsArray [i]);
+                                               item.SetDirty ();
+                                               items [i] = item;
+                                       }
+                               }
                        }
                }
 
                object IStateManager.SaveViewState() {
                        int count;
+                       bool itemsDirty = false;
+
                        count = items.Count;
-                       if (count == 0)
+                       if (count == 0 && !dirty)
                                return null;
 
-                       object [] state = new object [count];
+                       object [] itemsState = null;
+                       if (count > 0)
+                               itemsState = new object [count];
+
                        for (int i = 0; i < count; i++) {
-                               state [i] = ((IStateManager) items [i]).SaveViewState ();
-                               if (state [i] != null)
-                                       dirty = true;
+                               itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
+                               if (itemsState [i] != null)
+                                       itemsDirty = true;
                        }
 
-                       if (dirty)
-                               return state;
+                       if (!dirty && !itemsDirty)
+                               return null;
 
-                       return null;
+                       return new Pair (dirty, itemsState);
                }
 
                void IStateManager.TrackViewState() {
@@ -313,13 +337,17 @@ namespace System.Web.UI.WebControls {
                                ((ListItem)items[i]).TrackViewState();
                        }
                }
-               #endregion      // Interface methods
+#endregion     // Interface methods
 
-               private void SetDirty ()
+               void SetDirty ()
                {
                        dirty = true;
-                       for (int i = 0; i < items.Count; i++)
+                       for (int i = lastDirty; i < items.Count; i++)
                                ((ListItem) items [i]).SetDirty ();
+                       
+                       lastDirty = items.Count - 1;
+                       if (lastDirty < 0)
+                               lastDirty = 0;
                }
        }
 }