Jelajahi Sumber

2006-11-21 Igor Zelmanovich <[email protected]>

	* Page.cs: fixed: LoadControlState is called for controls 
	that added on Load and latter


svn path=/trunk/mcs/; revision=68241
Igor Zelmanovich 19 tahun lalu
induk
melakukan
7f254b9857

+ 5 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,8 @@
+2006-11-21 Igor Zelmanovich <[email protected]>
+
+	* Page.cs: fixed: LoadControlState is called for controls 
+	that added on Load and latter
+
 2006-11-20  Marek Habersack  <[email protected]>
 
 	* Control.cs: Implementations of a few missing properties.

+ 21 - 8
mcs/class/System.Web/System.Web.UI/Page.cs

@@ -73,6 +73,7 @@ public class Page : TemplateControl, IHttpHandler
 #if NET_2_0
 	private PageLifeCycle _lifeCycle = PageLifeCycle.Unknown;
 	private bool _eventValidation = true;
+	private object [] _savedControlState;
 #endif
 	private bool _viewState = true;
 	private bool _viewStateMac;
@@ -1458,17 +1459,15 @@ public class Page : TemplateControl, IHttpHandler
 		if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
 			return null;
 
-		_savedViewState = null;
 		if (view_state == "")
 			return null;
 
 		LosFormatter fmt = GetFormatter ();
 		try {
-			_savedViewState = fmt.Deserialize (view_state);
+			return fmt.Deserialize (view_state);
 		} catch (Exception e) {
 			throw new HttpException ("Error restoring page viewstate.", e);
 		}
-		return _savedViewState;
 	}
 
 	internal void LoadPageViewState()
@@ -1830,8 +1829,21 @@ public class Page : TemplateControl, IHttpHandler
 	[EditorBrowsable (EditorBrowsableState.Advanced)]
 	public void RegisterRequiresControlState (Control control)
 	{
-		if (requireStateControls == null) requireStateControls = new ArrayList ();
-		requireStateControls.Add (control);
+		if (control == null)
+			throw new ArgumentNullException ("control");
+
+		if (RequiresControlState (control))
+			return;
+
+		if (requireStateControls == null)
+			requireStateControls = new ArrayList ();
+		int n = requireStateControls.Add (control);
+
+		if (_savedControlState != null && n < _savedControlState.Length) {
+			object state = _savedControlState [n];
+			if (state != null)
+				control.LoadControlState (state);
+		}
 	}
 	
 	public bool RequiresControlState (Control control)
@@ -1888,13 +1900,14 @@ public class Page : TemplateControl, IHttpHandler
 	
 	void LoadPageControlState (object data)
 	{
+		_savedControlState = (object []) data;
+		
 		if (requireStateControls == null) return;
 
-		object[] state = (object[]) data;
-		int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
+		int max = Math.Min (requireStateControls.Count, _savedControlState != null ? _savedControlState.Length : requireStateControls.Count);
 		for (int n=0; n < max; n++) {
 			Control ctl = (Control) requireStateControls [n];
-			ctl.LoadControlState (state != null ? state [n] : null);
+			ctl.LoadControlState (_savedControlState != null ? _savedControlState [n] : null);
 		}
 	}
 

+ 4 - 0
mcs/class/System.Web/Test/System.Web.UI/ChangeLog

@@ -1,3 +1,7 @@
+2006-11-21  Igor Zelmanovich   <[email protected]>
+
+	* ControlTest.cs: added test.
+
 2006-11-20  Igor Zelmanovich   <[email protected]>
 
 	* ClientScriptManagerTest.cs: removed NotWorking attributes, fixed tests.

+ 57 - 0
mcs/class/System.Web/Test/System.Web.UI/ControlTest.cs

@@ -38,6 +38,7 @@ using System.Globalization;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
+using MonoTests.SystemWeb.Framework;
 
 namespace MonoTests.System.Web.UI
 {
@@ -176,6 +177,61 @@ namespace MonoTests.System.Web.UI
 			p.EnableViewState = false;
 			Assert.IsFalse (c.DoIsViewStateEnabled);
 		}
+
+		[Test]
+		[Category ("NunitWeb")]
+		public void ControlState () {
+			WebTest t = new WebTest (PageInvoker.CreateOnLoad (ControlState_Load));
+			t.Run ();
+			FormRequest fr = new FormRequest (t.Response, "form1");
+			fr.Controls.Add ("__EVENTTARGET");
+			fr.Controls.Add ("__EVENTARGUMENT");
+			fr.Controls ["__EVENTTARGET"].Value = "";
+			fr.Controls ["__EVENTARGUMENT"].Value = "";
+			t.Request = fr;
+			t.Run ();
+		}
+
+		public static void ControlState_Load (Page p) {
+			ControlWithState c1 = new ControlWithState ();
+			ControlWithState c2 = new ControlWithState ();
+			c1.Controls.Add (c2);
+			p.Form.Controls.Add (c1);
+			if (!p.IsPostBack) {
+				c1.State = "State";
+				c2.State = "Cool";
+			}
+			else {
+				ControlWithState c3 = new ControlWithState ();
+				p.Form.Controls.Add (c3);
+				Assert.AreEqual ("State", c1.State, "ControlState");
+				Assert.AreEqual ("Cool", c2.State, "ControlState");
+			}
+		}
+
+		class ControlWithState : Control
+		{
+			string _state;
+
+			public string State {
+				get { return _state; }
+				set { _state = value; }
+			}
+
+			protected override void OnInit (EventArgs e) {
+				base.OnInit (e);
+				Page.RegisterRequiresControlState (this);
+				Page.RegisterRequiresControlState (this);
+			}
+
+			protected override object SaveControlState () {
+				return State;
+			}
+
+			protected override void LoadControlState (object savedState) {
+				State = (string) savedState;
+			}
+		}
 #endif
 
 		[Test]
@@ -249,3 +305,4 @@ namespace MonoTests.System.Web.UI
 	}
 }
 
+