Explorar o código

2006-01-02 Igor Zelmanovich <[email protected]>

	* ListControl.cs: fixed SelectedValue and SelectedIndex properties.
	set accessor works differently in 2.0.		 


svn path=/trunk/mcs/; revision=70421
Igor Zelmanovich %!s(int64=19) %!d(string=hai) anos
pai
achega
bfb1ce8954

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

@@ -1,3 +1,8 @@
+2006-01-02 Igor Zelmanovich <[email protected]>
+
+	* ListControl.cs: fixed SelectedValue and SelectedIndex properties.
+	set accessor works differently in 2.0.		 
+
 2007-01-02  Vladimir Krasnov  <[email protected]>
 
 	* SqlDataSourceView.cs,ObjectDataSourceView.cs: fixed parameter merge

+ 106 - 62
mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs

@@ -60,9 +60,14 @@ namespace System.Web.UI.WebControls {
 #endif
 
 		private ListItemCollection items;
+#if NET_2_0
+		int _selectedIndex = -2;
+		string _selectedValue;
+#else		
 		int saved_selected_index = -2;
 		string saved_selected_value;
-		
+#endif
+
 		public ListControl () : base (HtmlTextWriterTag.Select)
 		{
 		}
@@ -193,6 +198,24 @@ namespace System.Web.UI.WebControls {
 				return -1;
 			}
 			set {
+#if NET_2_0
+				_selectedIndex = value;
+
+				if (value < -1)
+					throw new ArgumentOutOfRangeException ("value");
+
+				if (value >= Items.Count) 
+					return;
+
+				ClearSelection ();
+				if (value == -1)
+					return;
+
+				items [value].Selected = true;
+
+				/* you'd think this would be called, but noooo */
+				//OnSelectedIndexChanged (EventArgs.Empty);
+#else
 				if (items == null || items.Count == 0) {
 					// This will happen when assigning this property
 					// before DataBind () is called on the control.
@@ -200,13 +223,6 @@ namespace System.Web.UI.WebControls {
 					return;
 				}
 
-#if NET_2_0
-				if (value >= Items.Count) {
-					ClearSelection ();
-					return;
-				}
-#endif
-
 				if (value < -1 || value >= Items.Count)
 					throw new ArgumentOutOfRangeException ("value");
 
@@ -218,6 +234,7 @@ namespace System.Web.UI.WebControls {
 
 				/* you'd think this would be called, but noooo */
 				//OnSelectedIndexChanged (EventArgs.Empty);
+#endif
 			}
 		}
 
@@ -254,6 +271,10 @@ namespace System.Web.UI.WebControls {
 				return Items [si].Value;
 			}
 			set {
+#if NET_2_0
+				_selectedValue = value;
+				SetSelectedValue (value);
+#else
 				ClearSelection ();
 				if (items == null || items.Count == 0) {
 					// This will happen when assigning this property
@@ -272,7 +293,6 @@ namespace System.Web.UI.WebControls {
 					}
 				}
 
-#if !NET_2_0
 				if (thr) {
 					string msg = String.Format ("Argument value is out of range: {0}", value);
 					throw new ArgumentOutOfRangeException (msg);
@@ -282,6 +302,22 @@ namespace System.Web.UI.WebControls {
 		}
 
 #if NET_2_0
+		bool SetSelectedValue (string value)
+		{
+			if (items != null && items.Count > 0) {
+				int count = items.Count;
+				ListItemCollection coll = Items;
+				for (int i = 0; i < count; i++) {
+					if (coll [i].Value == value) {
+						ClearSelection ();
+						coll [i].Selected = true;
+						return true;
+					}
+				}
+			}
+			return false;
+		}
+
 		[Themeable (false)]
 		[DefaultValue ("")]
 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
@@ -334,10 +370,7 @@ namespace System.Web.UI.WebControls {
 
 #if !NET_2_0
 			IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
-			if (list == null)
-				return;
-
-			DoDataBinding (list);
+			PerformDataBinding (list);
 #endif
 		}
 
@@ -351,47 +384,72 @@ namespace System.Web.UI.WebControls {
 			base.OnPreRender (e);
 		}
 
-		void DoDataBinding (IEnumerable dataSource)
+#if NET_2_0
+		protected virtual void OnTextChanged (EventArgs e)
 		{
-			if (dataSource != null) {
+			EventHandler handler = (EventHandler) Events [TextChangedEvent];
+			if (handler != null)
+				handler (this, e);
+		}
+#endif		
+
 #if NET_2_0
-				if (!AppendDataBoundItems)
+		protected internal override
 #endif
-					Items.Clear ();
-
-				string format = DataTextFormatString;
-				if (format == "")
-					format = null;
-
-				string text_field = DataTextField;
-				string value_field = DataValueField;
-				ListItemCollection coll = Items;
-				foreach (object container in dataSource) {
-					string text;
-					string val;
+		void PerformDataBinding (IEnumerable dataSource)
+		{
+			if (dataSource == null)
+				return;
+#if NET_2_0
+			if (!AppendDataBoundItems)
+#endif
+				Items.Clear ();
+
+			string format = DataTextFormatString;
+			if (format == "")
+				format = null;
+
+			string text_field = DataTextField;
+			string value_field = DataValueField;
+			ListItemCollection coll = Items;
+			foreach (object container in dataSource) {
+				string text;
+				string val;
+
+				text = val = null;
+				if (text_field != "") {
+					text = DataBinder.GetPropertyValue (container, text_field, format);
+				}
 
-					text = val = null;
-					if (text_field != "") {
-						text = DataBinder.GetPropertyValue (container, text_field, format);
-					}
+				if (value_field != "") {
+					val = DataBinder.GetPropertyValue (container, value_field).ToString ();
+				}
+				else if (text_field == "") {
+					text = val = container.ToString ();
+					if (format != null)
+						text = String.Format (format, container);
+				}
+				else if (text != null) {
+					val = text;
+				}
 
-					if (value_field != "") {
-						val = DataBinder.GetPropertyValue (container, value_field).ToString ();
-					} else if (text_field == "") {
-						text = val = container.ToString ();
-						if (format != null)
-							text = String.Format (format, container);
-					} else if (text != null) {
-						val = text;
-					}
+				if (text == null)
+					text = val;
 
-					if (text == null)
-						text = val;
+				coll.Add (new ListItem (text, val));
+			}
 
-					coll.Add (new ListItem (text, val));
-				}
+#if NET_2_0
+			if (!String.IsNullOrEmpty (_selectedValue)) {
+				if (!SetSelectedValue (_selectedValue))
+					throw new ArgumentOutOfRangeException ("value", String.Format ("'{0}' has a SelectedValue which is invalid because it does not exist in the list of items.", ID));
+				if (_selectedIndex >= 0 && _selectedIndex != SelectedIndex)
+					throw new ArgumentException ("SelectedIndex and SelectedValue are mutually exclusive.");
+			}
+			else if (_selectedIndex >= 0) {
+				SelectedIndex = _selectedIndex;
 			}
-			
+#else
 			if (saved_selected_value != null) {
 				SelectedValue = saved_selected_value;
 				if (saved_selected_index != -2 && saved_selected_index != SelectedIndex)
@@ -401,25 +459,10 @@ namespace System.Web.UI.WebControls {
 				SelectedIndex = saved_selected_index;
 				// No need to check saved_selected_value here, as it's done before.
 			}
+#endif
 		}
 
 #if NET_2_0
-		protected virtual void OnTextChanged (EventArgs e)
-		{
-			EventHandler handler = (EventHandler) Events [TextChangedEvent];
-			if (handler != null)
-				handler (this, e);
-		}
-
-		protected internal override void PerformDataBinding (IEnumerable dataSource)
-		{
-			base.PerformDataBinding (dataSource);
-
-			if (dataSource == null)
-				return;
-			DoDataBinding (dataSource);
-		}
-
 		[MonoTODO ("why override?")]
 		protected override void PerformSelect ()
 		{
@@ -594,3 +637,4 @@ namespace System.Web.UI.WebControls {
 
 
 
+

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

@@ -1,3 +1,7 @@
+2007-01-03 Igor Zelmanovich <[email protected]>
+
+	* ListControlTest.cs: new tests were added.
+
 2007-01-02 Igor Zelmanovich <[email protected]>
 
 	* DetailsViewTest.cs: new tests, removed NotWorking attribute.

+ 143 - 5
mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs

@@ -36,10 +36,13 @@ using System.Globalization;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
+#if NET_2_0
 using MonoTests.stand_alone.WebHarness;
 using MonoTests.SystemWeb.Framework;
+#endif
 
-namespace MonoTests.System.Web.UI.WebControls {
+namespace MonoTests.System.Web.UI.WebControls 
+{
 
 	public class ListControlPoker : ListControl {
 
@@ -182,7 +185,7 @@ namespace MonoTests.System.Web.UI.WebControls {
 		[TestFixtureSetUp]
 		public void SetUp ()
 		{
-#if DOT_NET
+#if VISUAL_STUDIO
 			WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.ListControlPage.aspx", "ListControlPage.aspx");
 #else
 			WebTest.CopyResource (GetType (), "ListControlPage.aspx", "ListControlPage.aspx");
@@ -681,8 +684,10 @@ namespace MonoTests.System.Web.UI.WebControls {
 			list.Add (3);
 			p.DataSource = list;
 			p.SelectedIndex = 2;
+			Assert.AreEqual (-1, p.SelectedIndex, "#01");
 			p.DataBind ();
-			Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
+			Assert.AreEqual (2, p.SelectedIndex, "#02");
+			Assert.AreEqual (3.ToString (), p.SelectedValue, "#03");
 		}
 
 		[Test]
@@ -713,8 +718,9 @@ namespace MonoTests.System.Web.UI.WebControls {
 			// the same thing.
 			p.SelectedIndex = 2;
 			p.SelectedValue = "3";
+			Assert.AreEqual ("", p.SelectedValue, "#01");
 			p.DataBind ();
-			Assert.AreEqual ("3", p.SelectedValue, "#01");
+			Assert.AreEqual ("3", p.SelectedValue, "#02");
 		}
 
 		[Test]
@@ -745,7 +751,138 @@ namespace MonoTests.System.Web.UI.WebControls {
 			Assert.AreEqual (3, p.Items.Count, "#01");
 		}
 
-		class Data {
+#if NET_2_0
+		[Test]
+		public void DataBinding7 () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.DataBind ();
+
+			p.SelectedValue = "3";
+			Assert.AreEqual (2, p.SelectedIndex, "#01");
+			
+			p.DataBind ();
+			Assert.AreEqual ("3", p.SelectedValue, "#02");
+			Assert.AreEqual (2, p.SelectedIndex, "#03");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void DataBinding8 () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.DataBind ();
+
+			p.SelectedValue = "3";
+			Assert.AreEqual (2, p.SelectedIndex, "#01");
+
+			list = new ArrayList ();
+			list.Add (4);
+			list.Add (5);
+			list.Add (6);
+			p.DataSource = list;
+			p.DataBind ();
+			Assert.AreEqual ("3", p.SelectedValue, "#01");
+			Assert.AreEqual (2, p.SelectedIndex, "#01");
+		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentOutOfRangeException))]
+		public void DataBinding9 () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.SelectedValue = "5";
+			p.DataBind ();
+		}
+
+		[Test]
+		public void DataBinding1a () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.SelectedIndex = 4;
+			Assert.AreEqual (-1, p.SelectedIndex, "#01");
+			p.DataBind ();
+			Assert.AreEqual (-1, p.SelectedIndex, "#02");
+			Assert.AreEqual ("", p.SelectedValue, "#03");
+		}
+		
+		[Test]
+		public void DataBinding10 () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.DataBind ();
+
+			p.SelectedValue = "5";
+			Assert.AreEqual ("", p.SelectedValue, "#01");
+			
+			p.SelectedIndex = 4;
+			Assert.AreEqual (-1, p.SelectedIndex, "#02");
+		}
+
+		[Test]
+		public void DataBinding11 () {
+			ListControlPoker p = new ListControlPoker ();
+			ArrayList list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			p.DataSource = list;
+			p.SelectedValue = "3";
+			p.DataBind ();
+
+			p.SelectedValue = "5";
+			Assert.AreEqual ("3", p.SelectedValue, "#01");
+
+			p.SelectedIndex = 4;
+			Assert.AreEqual (2, p.SelectedIndex, "#02");
+
+			p.Items.Clear ();
+			Assert.AreEqual ("", p.SelectedValue, "#03");
+			Assert.AreEqual (-1, p.SelectedIndex, "#04");
+
+			p.Items.Add (new ListItem ("1"));
+			p.Items.Add (new ListItem ("2"));
+			p.Items.Add (new ListItem ("3"));
+			p.Items.Add (new ListItem ("4"));
+			p.Items.Add (new ListItem ("5"));
+			Assert.AreEqual ("", p.SelectedValue, "#05");
+			Assert.AreEqual (-1, p.SelectedIndex, "#06");
+			
+			list = new ArrayList ();
+			list.Add (1);
+			list.Add (2);
+			list.Add (3);
+			list.Add (4);
+			list.Add (5);
+			p.DataSource = list;
+			p.DataBind ();
+			Assert.AreEqual ("5", p.SelectedValue, "#07");
+			Assert.AreEqual (4, p.SelectedIndex, "#08");
+		}
+#endif
+
+		class Data 
+		{
 			string name;
 			object val;
 
@@ -898,3 +1035,4 @@ namespace MonoTests.System.Web.UI.WebControls {
 
 
 
+