New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ImageField.cs
index 3683be71b666b75bf1e04d776cfd7af650e39798..7513feafd010053c2f3561b6c8a27b672765ecab 100644 (file)
@@ -274,7 +274,7 @@ namespace System.Web.UI.WebControls {
                                if (dataItem == null)
                                        throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.");
                                if (fieldName == ThisExpression)
-                                       return dataItem.ToString ();
+                                       return dataItem;
                                else {
                                        if (cachedDescriptor != null) return cachedDescriptor.GetValue (dataItem);
                                        PropertyDescriptor prop = GetProperty (controlContainer, fieldName);
@@ -285,10 +285,15 @@ namespace System.Web.UI.WebControls {
                
                PropertyDescriptor GetProperty (Control controlContainer, string fieldName)
                {
+                       if (fieldName == ThisExpression)
+                               return null;
+                       
                        IDataItemContainer dic = (IDataItemContainer) controlContainer;
-                       PropertyDescriptor prop = TypeDescriptor.GetProperties (dic.DataItem) [fieldName];
+                       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (dic.DataItem);
+                       PropertyDescriptor prop = properties != null ? properties [fieldName] : null;
                        if (prop == null)
-                               new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
+                               throw new InvalidOperationException ("Property '" + fieldName + "' not found in object of type " + dic.DataItem.GetType());
+                       
                        return prop;
                }
                
@@ -299,34 +304,41 @@ namespace System.Web.UI.WebControls {
                
                protected virtual void OnDataBindField (object sender, EventArgs e)
                {
-                       DataControlFieldCell cell = (DataControlFieldCell) sender;\r
-\r
-                       if (cell.Controls.Count == 0)\r
+                       Control control = (Control) sender;
+                       ControlCollection controls = control != null ? control.Controls : null;
+                       Control namingContainer = control.NamingContainer;
+                       Control c;
+                       if (sender is DataControlFieldCell) {
+                               if (controls.Count == 0)
+                                       return;
+                               c = controls [0];
+                       } else if (sender is Image || sender is TextBox)
+                               c = control;
+                       else
                                return;
-                       
+
                        if (imageProperty == null)
-                               imageProperty = GetProperty (cell.BindingContainer, DataImageUrlField);
+                               imageProperty = GetProperty (namingContainer, DataImageUrlField);
                        
-                       Control c = cell.Controls [0];
                        if (c is TextBox) {
-                               object val = GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty);
-                               ((TextBox)c).Text = val != null ? val.ToString() : "";
+                               object val = GetValue (namingContainer, DataImageUrlField, ref imageProperty);
+                               ((TextBox)c).Text = val != null ? val.ToString() : String.Empty;
                        }
                        else if (c is Image) {
                                Image img = (Image)c;
-                               string value =  FormatImageUrlValue (GetValue (cell.BindingContainer, DataImageUrlField, ref imageProperty));
-                               if (value == null || (ConvertEmptyStringToNull && value.ToString ().Length == 0)) {
+                               string value =  FormatImageUrlValue (GetValue (namingContainer, DataImageUrlField, ref imageProperty));
+                               if (value == null || (ConvertEmptyStringToNull && value.Length == 0)) {
                                        if (NullImageUrl == null || NullImageUrl.Length == 0) {
                                                c.Visible = false;
                                                Label label = new Label ();
                                                label.Text = NullDisplayText;
-                                               cell.Controls.Add (label);
+                                               controls.Add (label);
                                        }
                                        else
                                                value = NullImageUrl;
                                }
                                img.ImageUrl = value;
-                               img.AlternateText = GetFormattedAlternateText (cell.BindingContainer);
+                               img.AlternateText = GetFormattedAlternateText (namingContainer);
                        }
                }