Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundColumn.cs
index e32cddf716968dcd59a56c692850934b61b83088..76a92eecfb18d5e6b928e0970902427c5cf9a742 100644 (file)
 //
 
 using System.ComponentModel;
+using System.Security.Permissions;
 
 namespace System.Web.UI.WebControls {
-       public class BoundColumn : DataGridColumn
-       {
 
-               private string data_format_string;
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public class BoundColumn : DataGridColumn 
+       {
+               string data_format_string;
 
                public BoundColumn ()
                {
@@ -89,11 +93,24 @@ namespace System.Web.UI.WebControls {
                {
                        base.InitializeCell (cell, columnIndex, itemType);
 
+                       string df = DataField;
+
                        switch (itemType) {
                        case ListItemType.Item:
-                       case ListItemType.EditItem:
+                       case ListItemType.SelectedItem:
                        case ListItemType.AlternatingItem:
-                               cell.DataBinding += new EventHandler (ItemDataBinding);
+                               if (df != null && df.Length != 0)
+                                       cell.DataBinding += new EventHandler (ItemDataBinding);
+                               break;
+                       case ListItemType.EditItem:
+                               if (ReadOnly && df != null && df.Length != 0) {
+                                       cell.DataBinding += new EventHandler (ItemDataBinding);
+                                       break;
+                               }
+                               TextBox tb = new TextBox ();
+                               if (df != null && df.Length != 0)
+                                       tb.DataBinding += new EventHandler (ItemDataBinding);
+                               cell.Controls.Add (tb);
                                break;
                        }
                }
@@ -109,22 +126,32 @@ namespace System.Web.UI.WebControls {
                        return String.Format (data_format_string, dataValue);
                }
 
-               private void ItemDataBinding (object sender, EventArgs e)
+               string GetValueFromItem (DataGridItem item)
                {
-                       TableCell cell = (TableCell) sender;
-                       DataGridItem item  = (DataGridItem) cell.NamingContainer;
-                       object value = null;
-
+                       object val;
                        if (DataField != thisExpr) {
-                               value = DataBinder.Eval (item.DataItem, DataField);
+                               val = DataBinder.Eval (item.DataItem, DataField);
                        } else {
-                               value = item.DataItem;
+                               val = item.DataItem;
                        }
 
-                       string text = FormatDataValue (value);
-                       if (text == String.Empty)
-                               text = " ";
-                       cell.Text = text;
+                       string text = FormatDataValue (val);
+                       return (text != "" ?  text : " ");
+               }
+
+               void ItemDataBinding (object sender, EventArgs e)
+               {
+                       Control ctrl = (Control) sender;
+                       string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
+
+                       TableCell cell = sender as TableCell;
+                       if (cell == null) {
+                               TextBox tb = (TextBox) sender;
+                               tb.Text = text;
+                       } else {
+                               cell.Text = text;
+                       }
                }
        }
 }
+