Author: krasnov
Date: 2007-07-05 10:43:44 -0400 (Thu, 05 Jul 2007)
New Revision: 81396

Modified:
   trunk/mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog
   trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs
   trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItem.cs
   trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItemCollection.cs
   trunk/mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs
Log:
* ListControl.cs:
* ListItem.cs:
* ListItemCollection.cs: controls based on ListControl should not save view 
state before than TrackViewState called

Modified: trunk/mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog      
2007-07-05 14:38:05 UTC (rev 81395)
+++ trunk/mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog      
2007-07-05 14:43:44 UTC (rev 81396)
@@ -1,3 +1,10 @@
+2007-07-05  Vladimir Krasnov  <[EMAIL PROTECTED]>
+
+       * ListControl.cs:
+       * ListItem.cs:
+       * ListItemCollection.cs: controls based on ListControl should not save
+       view state before than TrackViewState called
+
 2007-07-04  Vladimir Krasnov  <[EMAIL PROTECTED]>
 
        * WebControl.cs: fixed AddDisplayStyleAttribute, returns if

Modified: trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs 
2007-07-05 14:38:05 UTC (rev 81395)
+++ trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs 
2007-07-05 14:43:44 UTC (rev 81396)
@@ -170,8 +170,11 @@
                [WebCategory ("Misc")]
                public virtual ListItemCollection Items {
                        get {
-                               if (items == null)
+                               if (items == null) {
                                        items = new ListItemCollection ();
+                                       if (IsTrackingViewState)
+                                               ((IStateManager) 
items).TrackViewState ();
+                               }
                                return items;
                        }
                }
@@ -536,7 +539,7 @@
                                second = manager.SaveViewState ();
 
                        ArrayList selected = GetSelectedIndicesInternal ();
-                       if (first == null && second == null && selected == null)
+                       if (first == null && second == null && (selected == 
null || selected.Count == 0))
                                return null;
 
                        return new Triplet (first, second, selected);

Modified: trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItem.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItem.cs    
2007-07-05 14:38:05 UTC (rev 81395)
+++ trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItem.cs    
2007-07-05 14:43:44 UTC (rev 81396)
@@ -47,8 +47,8 @@
 #endif
                public ListItem (string text, string value)
                {
-                       Text = text;
-                       Value = value;
+                       this.text = text;
+                       this.value = value;
                }
        
                public ListItem (string text) : this (text, null)
@@ -108,6 +108,9 @@
                
                internal void LoadViewState (object state)
                {
+                       if (state == null)
+                               return;
+
                        object [] states = (object []) state;
 
                        if (states [0] != null) sb = (StateBag) states[0];
@@ -125,6 +128,9 @@
 
                internal object SaveViewState ()
                {
+                       if (!dirty)
+                               return null;
+
 #if NET_2_0
                        object [] state = new object [4];
 #else
@@ -205,6 +211,8 @@
                
                        set {
                                text = value;
+                               if (tracking)
+                                       SetDirty ();
                        }
                }
 
@@ -221,14 +229,26 @@
                
                        set {
                                this.value = value;
+                               if (tracking)
+                                       SetDirty ();
                        }
                }
 
+               internal void SetDirty ()
+               {
+                       dirty = true;
+               }
+
 #if NET_2_0
                public bool Enabled
                {
                        get { return enabled; }
-                       set { enabled = value; }
+                       set
+                       {
+                               enabled = value;
+                               if (tracking)
+                                       SetDirty ();
+                       }
                }
 
                internal bool HasAttributes {
@@ -239,6 +259,7 @@
                string text;
                string value;
                bool selected;
+               bool dirty;
 #if NET_2_0
                bool enabled = true;
 #endif

Modified: 
trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItemCollection.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItemCollection.cs  
2007-07-05 14:38:05 UTC (rev 81395)
+++ trunk/mcs/class/System.Web/System.Web.UI.WebControls/ListItemCollection.cs  
2007-07-05 14:43:44 UTC (rev 81396)
@@ -40,6 +40,7 @@
                #region Fields
                private ArrayList       items;
                private bool            tracking;
+               private bool            dirty;
                #endregion      // Fields
 
                #region Public Constructors
@@ -93,20 +94,38 @@
                #region Public Instance Methods
                public void Add(ListItem item) {
                        items.Add(item);
+                       if (tracking) {
+                               item.TrackViewState ();
+                               SetDirty ();
+                       }
                }
 
                public void Add(string item) {
-                       items.Add(new ListItem(item));
+                       ListItem listItem = new ListItem (item);
+                       items.Add (listItem);
+
+                       if (tracking) {
+                               listItem.TrackViewState ();
+                               SetDirty ();
+                       }
                }
 
                public void AddRange(ListItem[] items) {
                        for (int i = 0; i < items.Length; i++) {
                                Add(items[i]);
+
+                               if (tracking) {
+                                       items [i].TrackViewState ();
+                                       SetDirty ();
+                               }
                        }
                }
 
                public void Clear() {
                        items.Clear();
+
+                       if (tracking)
+                               SetDirty ();
                }
 
                public bool Contains(ListItem item) {
@@ -152,25 +171,46 @@
 
                public void Insert(int index, ListItem item) {
                        items.Insert(index, item);
+
+                       if (tracking) {
+                               item.TrackViewState ();
+                               SetDirty ();
+                       }
                }
 
                public void Insert(int index, string item) {
-                       items.Insert(index, new ListItem(item));
+                       ListItem listItem = new ListItem(item);
+                       items.Insert (index, listItem);
+
+                       if (tracking) {
+                               listItem.TrackViewState ();
+                               SetDirty ();
+                       }
                }
 
                public void Remove(ListItem item) {
                        items.Remove(item);
+                       
+                       if (tracking)
+                               SetDirty ();
                }
 
                public void Remove (string item)
                {
                        for (int i = 0; i < items.Count; i++)
-                               if (item == this [i].Value)
+                               if (item == this [i].Value) {
                                        items.RemoveAt (i);
+
+                                       if (tracking)
+                                               SetDirty ();
+                               }
                }
 
                public void RemoveAt(int index) {
                        items.RemoveAt(index);
+
+                       if (tracking)
+                               SetDirty ();
                }
                #endregion      // Public Instance Methods
 
@@ -189,12 +229,21 @@
                        set {
                                if ((index >= 0) && (index < items.Count)) {
                                        items[index] = (ListItem)value;
+
+                                       if (tracking)
+                                               ((ListItem) 
value).TrackViewState ();
                                }
                        }
                }
 
                int IList.Add(object value) {
-                       return items.Add((ListItem)value);
+                       int i = items.Add ((ListItem) value);
+
+                       if (tracking) {
+                               ((IStateManager) value).TrackViewState ();
+                               SetDirty ();
+                       }
+                       return i;
                }
 
                bool IList.Contains(object value) {
@@ -220,73 +269,41 @@
                }
 
                void IStateManager.LoadViewState(object state) {
-                       int     count;
-                       string[] text;
-                       string[] value;
-#if NET_2_0
-                       bool [] enabled;
-#endif
 
-                       if (state == null) {
+                       if (state == null)
                                return;
-                       }
-#if NET_2_0
-                       Triplet stateObj;
-                       stateObj = (Triplet) state;
-#else
-                       Pair stateObj;
-                       stateObj = (Pair) state;
-#endif
 
-                       text = (string []) stateObj.First;
-                       value = (string []) stateObj.Second;
-#if NET_2_0
-                       enabled = (bool []) stateObj.Third;
-#endif
+                       object [] stateObj = (object[]) state;
+                       int count = stateObj.Length;
 
-                       count = text.Length;
+                       items = new ArrayList(count);
 
-                       items = new ArrayList(count);
                        for (int i = 0; i < count; i++) {
-#if NET_2_0
-                               items.Add(new ListItem(text[i], value[i], 
enabled[i]));
-#else
-                               items.Add(new ListItem(text[i], value[i]));
-#endif
+                               ListItem item = new ListItem ();
+                               if (stateObj [i] != null)
+                                       item.LoadViewState (stateObj [i]);
+
+                               items.Add (item);
                        }
                }
 
                object IStateManager.SaveViewState() {
                        int count;
-                       string[] text;
-                       string[] value;
-#if NET_2_0
-                       bool [] enabled;
-#endif
-
                        count = items.Count;
                        if (count == 0)
                                return null;
-                       
-                       text = new string[count];
-                       value = new string[count];
-#if NET_2_0
-                       enabled = new bool [count];
-#endif
 
+                       object [] state = new object [count];
                        for (int i = 0; i < count; i++) {
-                               text[i] = ((ListItem)items[i]).Text;
-                               value[i] = ((ListItem)items[i]).Value;
-#if NET_2_0
-                               enabled [i] = ((ListItem) items [i]).Enabled;
-#endif
+                               state [i] = ((IStateManager) items 
[i]).SaveViewState ();
+                               if (state [i] != null)
+                                       dirty = true;
                        }
 
-#if NET_2_0
-                       return new Triplet(text, value, enabled);
-#else
-                       return new Pair(text, value);
-#endif
+                       if (dirty)
+                               return state;
+
+                       return null;
                }
 
                void IStateManager.TrackViewState() {
@@ -297,5 +314,12 @@
                        }
                }
                #endregion      // Interface methods
+
+               private void SetDirty ()
+               {
+                       dirty = true;
+                       for (int i = 0; i < items.Count; i++)
+                               ((ListItem) items [i]).SetDirty ();
+               }
        }
 }

Modified: 
trunk/mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs
===================================================================
--- 
trunk/mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs    
    2007-07-05 14:38:05 UTC (rev 81395)
+++ 
trunk/mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs    
    2007-07-05 14:43:44 UTC (rev 81396)
@@ -559,6 +559,20 @@
                }
 
                [Test]
+               // Tests Save/Load/Track ViewState
+               public void ViewStateNotNeeded ()
+               {
+                       ListControlPoker a = new ListControlPoker ();
+
+                       a.Items.Add ("a");
+                       a.Items.Add ("b");
+                       a.Items.Add ("c");
+
+                       object state = a.SaveState ();
+                       Assert.AreEqual (null, state, "A1");
+               }
+
+               [Test]
                public void ViewStateContents ()
                {
                        ListControlPoker p = new ListControlPoker ();

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to