Prechádzať zdrojové kódy

2008-10-08 Marek Habersack <[email protected]>

	* ControlParameter.cs: Evaluate calls DataBinder.Eval to do the
	evaluation now. This makes it support complex expressions.

2008-10-08  Marek Habersack  <[email protected]>

	* DataBinder.cs: in Eval expression needs to be trimmed before
	checking whether it's an empty string.
	GetIndexedPropertyValue must explicitly look for the "Item"
	property, the lack of the DefaultMember attribute on type must not
	throw exceptions.

2008-10-08  Marek Habersack  <[email protected]>

	* ControlParameterTest.cs: added a test for ControlParameter
	evaluating a complex property expression.

svn path=/trunk/mcs/; revision=115193
Marek Habersack 17 rokov pred
rodič
commit
7bbd8d5027

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

@@ -1,3 +1,8 @@
+2008-10-08  Marek Habersack  <[email protected]>
+
+	* ControlParameter.cs: Evaluate calls DataBinder.Eval to do the
+	evaluation now. This makes it support complex expressions.
+
 2008-09-30  Sebastien Pouliot  <[email protected]> 
 
 	* RoleGroupCollection.cs: Fix recursive calls (wrong target)

+ 6 - 7
mcs/class/System.Web/System.Web.UI.WebControls/ControlParameter.cs

@@ -74,8 +74,10 @@ namespace System.Web.UI.WebControls {
 		
 		protected override object Evaluate (HttpContext ctx, Control control)
 		{
-			if (control == null) return null;
-			if (control.Page == null) return null;
+			if (control == null)
+				return null;
+			if (control.Page == null)
+				return null;
 			
 			if(String.IsNullOrEmpty(ControlID))
 				throw new ArgumentException ("The ControlID property is not set.");
@@ -98,12 +100,9 @@ namespace System.Web.UI.WebControls {
 					throw new ArgumentException ("The PropertyName property is not set and the Control identified by the ControlID property is not decorated with a ControlValuePropertyAttribute attribute.");
 				ControlValuePropertyAttribute attr = (ControlValuePropertyAttribute) attrs [0];
 				propName = attr.Name;
-			}
-			PropertyInfo prop = c.GetType ().GetProperty (propName);
-			if (prop == null)
-				throw new InvalidOperationException ("Property '" + propName + "' not found in type '" + c.GetType () + "'.");
+ 			}
 			
-			return prop.GetValue (c, null);
+			return DataBinder.Eval (c, propName);
 		}
 		
 		[WebCategoryAttribute ("Control")]

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

@@ -1,3 +1,11 @@
+2008-10-08  Marek Habersack  <[email protected]>
+
+	* DataBinder.cs: in Eval expression needs to be trimmed before
+	checking whether it's an empty string.
+	GetIndexedPropertyValue must explicitly look for the "Item"
+	property, the lack of the DefaultMember attribute on type must not
+	throw exceptions.
+
 2008-10-03  Marek Habersack  <[email protected]>
 
 	* TemplateParser.cs: make sure the generated class name is a valid

+ 6 - 4
mcs/class/System.Web/System.Web.UI/DataBinder.cs

@@ -58,7 +58,8 @@ namespace System.Web.UI {
 		
 		public static object Eval (object container, string expression)
 		{
-			if ((expression == null) || (expression.Length == 0))
+			expression = expression != null ? expression.Trim () : null;
+			if (expression == null || expression.Length == 0)
 				throw new ArgumentNullException ("expression");
 
 			object current = container;
@@ -144,12 +145,13 @@ namespace System.Web.UI {
 			}
 
 			Type t = container.GetType ();
+
 			// MS does not seem to look for any other than "Item"!!!
 			object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
 			if (atts.Length != 1)
-				throw new ArgumentException (expr + " indexer not found.");
-
-			property = ((DefaultMemberAttribute) atts [0]).MemberName;
+				property = "Item";
+			else
+				property = ((DefaultMemberAttribute) atts [0]).MemberName;
 
 			Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
 			PropertyInfo prop = t.GetProperty (property, argTypes);

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

@@ -1,3 +1,8 @@
+2008-10-08  Marek Habersack  <[email protected]>
+
+	* ControlParameterTest.cs: added a test for ControlParameter
+	evaluating a complex property expression.
+
 2008-09-30  Sebastien Pouliot  <[email protected]>
 
 	* RoleGroupCollectionTest.cs: Add test case this[int].

+ 76 - 44
mcs/class/System.Web/Test/System.Web.UI.WebControls/ControlParameterTest.cs

@@ -31,6 +31,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.Collections.Specialized;
 using System.Text;
 using NUnit.Framework;
 using System.Web;
@@ -39,63 +40,77 @@ using System.Web.UI.WebControls;
 
 namespace MonoTests.System.Web.UI.WebControls
 {
-    public class ControlParameterPoker : ControlParameter
-    {
-	    public ControlParameterPoker (ControlParameter control)
-		    : base (control)
-	    {
-	    }
+	public class ControlParameterPoker : ControlParameter
+	{
+		public ControlParameterPoker (ControlParameter control)
+			: base (control)
+		{
+		}
 	    
-	    public ControlParameterPoker (string name,TypeCode type, string controlID,string propertyName)
-		    :base(name,type,controlID,propertyName)
-	    {
-	    }
+		public ControlParameterPoker (string name,TypeCode type, string controlID,string propertyName)
+			:base(name,type,controlID,propertyName)
+		{
+		}
 
-            public ControlParameterPoker (string name, string controlId, string propertyName)
+		public ControlParameterPoker (string name, string controlId, string propertyName)
 			: base (name, controlId, propertyName)
-	    {
-	    }
+		{
+		}
 
 	
-	    public ControlParameterPoker (string name, string controlId)
+		public ControlParameterPoker (string name, string controlId)
 			: base (name, controlId)	
-	    {	
-	    }
+		{	
+		}
        
-	    public ControlParameterPoker() // constructor       
-	    {        
-		    TrackViewState ();       
-	    }
-
-	    public object DoEvaluate (HttpContext context,Control control)
-	    {
-		    return base.Evaluate (context,control);
-	    }
-
-	    public Parameter DoClone ()
-	    {
-		    return base.Clone ();
-	    }
+		public ControlParameterPoker() // constructor       
+		{        
+			TrackViewState ();       
+		}
+
+		public object DoEvaluate (HttpContext context,Control control)
+		{
+			return base.Evaluate (context,control);
+		}
+
+		public Parameter DoClone ()
+		{
+			return base.Clone ();
+		}
 	
-	    public object SaveState ()		
-	    {	
-		    return SaveViewState ();		
-	    }
+		public object SaveState ()		
+		{	
+			return SaveViewState ();		
+		}
 
        
-	    public void LoadState (object o)       
-	    {       
-		    LoadViewState (o);      
-	    }
+		public void LoadState (object o)       
+		{       
+			LoadViewState (o);      
+		}
        
-	    public StateBag StateBag       
-	    {       
-		    get { return base.ViewState; }      
-	    }
+		public StateBag StateBag       
+		{       
+			get { return base.ViewState; }      
+		}
        
-    }
+	}
 
-    [TestFixture]
+	class TestControl : Control 
+	{
+		DataKey _values;
+		
+		public DataKey Values {
+			get { return _values; }
+		}
+
+		public TestControl (DataKey values)
+		{
+			this._values = values;
+		}
+	}
+	
+	[TestFixture]
 	public class ControlParameterTest
 	{
 		[Test]
@@ -163,6 +178,23 @@ namespace MonoTests.System.Web.UI.WebControls
 			Assert.AreEqual ("TestNewValue", value, "EvaluateValue2");
 		}
 
+		[Test]
+		public void ControlParameter_EvaluateComplex ()
+		{
+			ControlParameterPoker ctrlParam = new ControlParameterPoker ("Test", "TestControl1", "Values['one']");
+			Page page = new Page ();
+			
+			OrderedDictionary dict = new OrderedDictionary ();
+			dict.Add ("one", "1");
+			
+			DataKey values = new DataKey (dict);
+			TestControl test = new TestControl (values);
+			test.ID = "TestControl1";
+			page.Controls.Add (test);
+			string value = ctrlParam.DoEvaluate (HttpContext.Current, test) as string;
+			Assert.AreEqual ("1", value, "#1");
+		}
+		
 		[Test]
 		[ExpectedException (typeof (ArgumentException))]
 		public void EvaluateArgumemtException ()