Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundColumn.cs
old mode 100755 (executable)
new mode 100644 (file)
index 04654f8..76a92ee
@@ -1,14 +1,10 @@
 //
 // System.Web.UI.WebControls.BoundColumn.cs
 //
-// Authors:
-//   Gaurav Vaish (gvaish@iitk.ac.in)
-//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+// Author:
+//      Dick Porter  <dick@ximian.com>
 //
-// (C) Gaurav Vaish (2002)
-// (C) 2003 Andreas Nahr
-//\r
-
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.ComponentModel;
-using System.Web;
-using System.Web.UI;
+using System.Security.Permissions;
 
-namespace System.Web.UI.WebControls
-{
-       public class BoundColumn : DataGridColumn
-       {
-               public static readonly string thisExpr = "!";
+namespace System.Web.UI.WebControls {
 
-               private bool boundFieldDescriptionValid;
-               private string boundField;
-               private string formatting;
-               
-               private PropertyDescriptor desc;
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public class BoundColumn : DataGridColumn 
+       {
+               string data_format_string;
 
-               public BoundColumn(): base()
+               public BoundColumn ()
                {
                }
 
-               public override void Initialize()
-               {
-                       base.Initialize();
-
-                       desc       = null;
-                       boundField = DataField;
-                       formatting = DataFormatString;
-                       boundFieldDescriptionValid = false;
-               }
+               public static readonly string thisExpr = "!";
 
-               public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
+               [DefaultValue("")]
+               [WebSysDescription ("")]
+               [WebCategory ("Misc")]
+               public virtual string DataField 
                {
-                       base.InitializeCell(cell, columnIndex, itemType);
-
-                       Control bindCtrl = null;
-                       Control toAdd    = null;
-                       switch(itemType)
-                       {
-                               case ListItemType.Item : goto case ListItemType.SelectedItem;
-                               case ListItemType.AlternatingItem
-                                                      : goto case ListItemType.SelectedItem;
-                               case ListItemType.SelectedItem
-                                                      : if(DataField.Length != 0)
-                                                               bindCtrl = cell;
-                                                        break;
-                               case ListItemType.EditItem
-                                                          : if(!ReadOnly)
-                                                            {
-                                                               TextBox box = new TextBox();
-                                                               toAdd = box;
-                                                               if(DataField.Length != 0)
-                                                                       bindCtrl = box;
-                                                            } else 
-                                                                    goto case ListItemType.SelectedItem;
-
-                                                            break;
+                       get {
+                               return ViewState.GetString ("DataField", String.Empty);
+                       }
+                       set {
+                               ViewState ["DataField"] = value;
                        }
-                       if(toAdd != null)
-                               cell.Controls.Add(toAdd);
-                       if(bindCtrl != null)
-                               bindCtrl.DataBinding += new EventHandler(OnDataBindColumn);
-                       //throw new NotImplementedException();
                }
-
-               private void OnDataBindColumn(object sender, EventArgs e)
+               
+               [DefaultValue("")]
+               [WebSysDescription ("")]
+               [WebCategory ("Misc")]
+               public virtual string DataFormatString 
                {
-                       Control senderCtrl = (Control)sender;
-                       DataGridItem item  = (DataGridItem)senderCtrl.NamingContainer;
-                       object       data  = item.DataItem;
-
-                       if(!boundFieldDescriptionValid)
-                       {
-                               if(boundField != BoundColumn.thisExpr)
-                               {
-                                       desc = TypeDescriptor.GetProperties(data).Find(boundField, true);
-                                       if(desc == null && !DesignMode)
-                                       {
-                                               throw new HttpException(
-                                                         HttpRuntime.FormatResourceString("File_Not_Found",
-                                                                                          boundField));
-                                       }
-                                       boundFieldDescriptionValid = true;
-                               }
-                       }
-                       object value = data;
-                       string val = String.Empty;
-                       if(desc == null && DesignMode)
-                       {
-                               throw new NotImplementedException();
-                       } else
-                       {
-                               if(desc != null)
-                                       value = desc.GetValue(data);
-                               val = FormatDataValue(value);
+                       get {
+                               return ViewState.GetString ("DataFormatString", String.Empty);
                        }
-                       if(senderCtrl is TableCell)
-                       {
-                               if(val.Length == 0)
-                                       val = "&nbsp;";
-                               ((TableCell)senderCtrl).Text = val;
-                       } else
-                       {
-                               ((TextBox)senderCtrl).Text = val;
+                       set {
+                               ViewState ["DataFormatString"] = value;
                        }
                }
 
-               [DefaultValue (""), WebCategory ("Misc")]
-               [WebSysDescription ("The field that this column is bound to.")]
-               public virtual string DataField
+               [DefaultValue(false)]
+               [WebSysDescription ("")]
+               [WebCategory ("Misc")]
+               public virtual bool ReadOnly 
                {
-                       get
-                       {
-                               object o = ViewState["DataField"];
-                               if(o != null)
-                                       return (string)o;
-                               return String.Empty;
+                       get {
+                               return ViewState.GetBool ("ReadOnly", false);
                        }
-                       set
-                       {
-                               ViewState["DataField"] = value;
-                               OnColumnChanged();
+                       set {
+                               ViewState ["ReadOnly"] = value;
                        }
                }
+               
+               public override void Initialize ()
+               {
+                       data_format_string = DataFormatString;
+               }
 
-               [DefaultValue (""), WebCategory ("Misc")]
-               [WebSysDescription ("A format string that is applied to the data value.")]
-               public virtual string DataFormatString
+               public override void InitializeCell (TableCell cell, int columnIndex,
+                               ListItemType itemType)
                {
-                       get
-                       {
-                               object o = ViewState["DataFormatString"];
-                               if(o != null)
-                                       return (string)o;
-                               return String.Empty;
-                       }
-                       set
-                       {
-                               ViewState["DataFormatString"] = value;
-                               OnColumnChanged();
+                       base.InitializeCell (cell, columnIndex, itemType);
+
+                       string df = DataField;
+
+                       switch (itemType) {
+                       case ListItemType.Item:
+                       case ListItemType.SelectedItem:
+                       case ListItemType.AlternatingItem:
+                               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;
                        }
                }
 
-               [DefaultValue (false), WebCategory ("Misc")]
-               [WebSysDescription ("Determines if the databound field can only be displayed or also edited.")]
-               public virtual bool ReadOnly
+               protected virtual string FormatDataValue (object dataValue)
                {
-                       get
-                       {
-                               object o = ViewState["ReadOnly"];
-                               if(o != null)
-                                       return (bool)o;
-                               return false;
-                       }
-                       set
-                       {
-                               ViewState["ReadOnly"] = value;
+                       if (dataValue == null)
+                               return "";
+
+                       if (data_format_string == String.Empty)
+                               return dataValue.ToString ();
+
+                       return String.Format (data_format_string, dataValue);
+               }
+
+               string GetValueFromItem (DataGridItem item)
+               {
+                       object val;
+                       if (DataField != thisExpr) {
+                               val = DataBinder.Eval (item.DataItem, DataField);
+                       } else {
+                               val = item.DataItem;
                        }
+
+                       string text = FormatDataValue (val);
+                       return (text != "" ?  text : "&nbsp;");
                }
 
-               protected virtual string FormatDataValue(Object dataValue)
+               void ItemDataBinding (object sender, EventArgs e)
                {
-                       string retVal = String.Empty;
-                       if(dataValue != null)
-                       {
-                               if(formatting.Length == 0)
-                                       retVal = dataValue.ToString();
-                               else
-                                       retVal = String.Format(formatting, dataValue);
+                       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;
                        }
-                       return retVal;
                }
        }
 }
+