Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListItemCollection.cs
index 505d66eb58d61a7a11804f844b766c64b8245612..b0d19ca082776497df4fe166e025b491ee4cc6b8 100644 (file)
@@ -36,21 +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;
+       public sealed class ListItemCollection : IList, ICollection, IEnumerable, IStateManager 
+       {
+#region Fields
+               ArrayList items;
+               bool tracking;
+               bool dirty;
                int lastDirty = 0;
-               #endregion      // Fields
+#endregion     // Fields
 
-               #region Public Constructors
+#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;
@@ -215,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;
@@ -271,23 +272,28 @@ namespace System.Web.UI.WebControls {
                        }
                }
 
-               void IStateManager.LoadViewState (object savedState) {
+               void IStateManager.LoadViewState (object savedState)
+               {
                        Pair pair = savedState as Pair;
                        if (pair == null)
                                return;
 
                        bool newCollection = (bool) pair.First;
                        object [] itemsArray = (object []) pair.Second;
-                       int count = itemsArray.Length;
+                       int count = itemsArray==null ? 0 : itemsArray.Length;
 
                        if (newCollection)
-                               items = new ArrayList(count);
+                               if (count > 0)
+                                       items = new ArrayList(count);
+                               else
+                                       items = new ArrayList();
 
                        for (int i = 0; i < count; i++) {
                                ListItem item = new ListItem ();
                                
                                if (newCollection) {
                                        item.LoadViewState (itemsArray [i]);
+                                       item.SetDirty ();
                                        Add (item);
                                }
                                else{
@@ -305,10 +311,13 @@ namespace System.Web.UI.WebControls {
                        bool itemsDirty = false;
 
                        count = items.Count;
-                       if (count == 0)
+                       if (count == 0 && !dirty)
                                return null;
 
-                       object [] itemsState = new object [count];
+                       object [] itemsState = null;
+                       if (count > 0)
+                               itemsState = new object [count];
+
                        for (int i = 0; i < count; i++) {
                                itemsState [i] = ((IStateManager) items [i]).SaveViewState ();
                                if (itemsState [i] != null)
@@ -328,15 +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 = lastDirty; i < items.Count; i++)
                                ((ListItem) items [i]).SetDirty ();
-
-                       lastDirty = items.Count;
+                       
+                       lastDirty = items.Count - 1;
+                       if (lastDirty < 0)
+                               lastDirty = 0;
                }
        }
 }