[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;
if (tracking)
SetDirty ();
}
- #endregion // Public Instance Methods
+#endregion // Public Instance Methods
- #region Interface methods
+#region Interface methods
bool IList.IsFixedSize {
get {
return items.IsFixedSize;
}
}
- 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() {
((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;
}
}
}