* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / BoundField.cs
index 7660087d2d38f20b69a58a23fc63f18aaf655bbd..fd7ab51d78782327469cea47ed5ce44dfb8dd420 100644 (file)
@@ -45,15 +45,24 @@ namespace System.Web.UI.WebControls {
                public static readonly string ThisExpression = "!";
                
                PropertyDescriptor boundProperty;
+
+               [DefaultValueAttribute (false)]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Behavior")]
+               public virtual bool ApplyFormatInEditMode {
+                       get {
+                               return ViewState.GetBool ("ApplyFormatInEditMode", false);
+                       }
+                       set {
+                               ViewState ["ApplyFormatInEditMode"] = value;
+                       }
+               }
                
                [DefaultValueAttribute (true)]
+               [WebSysDescription ("")]
                [WebCategoryAttribute ("Behavior")]
                public virtual bool ConvertEmptyStringToNull {
-                       get {
-                               object ob = ViewState ["ConvertEmptyStringToNull"];
-                               if (ob != null) return (bool) ob;
-                               return true;
-                       }
+                       get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); }
                        set {
                                ViewState ["ConvertEmptyStringToNull"] = value;
                                OnFieldChanged ();
@@ -61,14 +70,11 @@ namespace System.Web.UI.WebControls {
                }
 
                [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
+               [WebSysDescription ("")]
                [WebCategoryAttribute ("Data")]
                [DefaultValueAttribute ("")]
                public virtual string DataField {
-                       get {
-                               object ob = ViewState ["DataField"];
-                               if (ob != null) return (string) ob;
-                               return "";
-                       }
+                       get { return ViewState.GetString ("DataField", ""); }
                        set {
                                ViewState ["DataField"] = value;
                                OnFieldChanged ();
@@ -76,59 +82,60 @@ namespace System.Web.UI.WebControls {
                }
 
                [DefaultValueAttribute ("")]
+               [WebSysDescription ("")]
                [WebCategoryAttribute ("Data")]
                public virtual string DataFormatString {
-                       get {
-                               object ob = ViewState ["DataFormatString"];
-                               if (ob != null) return (string) ob;
-                               return "";
-                       }
+                       get { return ViewState.GetString ("DataFormatString", ""); }
                        set {
                                ViewState ["DataFormatString"] = value;
                                OnFieldChanged ();
                        }
                }
 
+               [MonoTODO]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Appearance")]
+               public override string HeaderText {
+                       get { return ViewState.GetString ("HeaderText", "");
+                       }
+                       set {
+                               ViewState["HeaderText"] = value;
+                               OnFieldChanged ();
+                       }
+               }
+
                [DefaultValueAttribute ("")]
                [WebCategoryAttribute ("Behavior")]
                public virtual string NullDisplayText {
-                       get {
-                               object ob = ViewState ["NullDisplayText"];
-                               if (ob != null) return (string) ob;
-                               return "";
-                       }
+                       get { return ViewState.GetString ("NullDisplayText", ""); }
                        set {
                                ViewState ["NullDisplayText"] = value;
                                OnFieldChanged ();
                        }
                }
 
-               [WebCategoryAttribute ("Behavior")]
                [DefaultValueAttribute (false)]
-               public bool ReadOnly {
-                       get {
-                               object val = ViewState ["ReadOnly"];
-                               return val != null ? (bool) val : false;
-                       }
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("Behavior")]
+               public virtual bool ReadOnly {
+                       get { return ViewState.GetBool ("ReadOnly", false); }
                        set { 
                                ViewState ["ReadOnly"] = value;
                                OnFieldChanged ();
                        }
                }
 
-               [WebCategoryAttribute ("HtmlEncode")]
                [DefaultValueAttribute (true)]
+               [WebSysDescription ("")]
+               [WebCategoryAttribute ("HtmlEncode")]
                public virtual bool HtmlEncode {
-                       get {
-                               object val = ViewState ["HtmlEncode"];
-                               return val != null ? (bool) val : true;
-                       }
+                       get { return ViewState.GetBool ("HtmlEncode", true); }
                        set { 
-                               ViewState ["HtmlEncode"] = true;
+                               ViewState ["HtmlEncode"] = value;
                                OnFieldChanged ();
                        }
                }
-               
+
                public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
                        DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
                {
@@ -142,7 +149,14 @@ namespace System.Web.UI.WebControls {
                                dictionary [DataField] = cell.Text;
                        }
                }
-               
+
+               [MonoTODO]
+               public override bool Initialize (bool enableSorting, 
+                                                Control control)
+               {
+                       return base.Initialize (enableSorting, control);
+               }
+
                public override void InitializeCell (DataControlFieldCell cell,
                        DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
                {
@@ -154,11 +168,12 @@ namespace System.Web.UI.WebControls {
                        }
                }
                
-               public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
+               protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
                {
                        bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
                        if (editable && !ReadOnly) {
                                TextBox box = new TextBox ();
+                               box.ID = cell.ClientID;
                                cell.Controls.Add (box);
                        }
                }
@@ -170,8 +185,14 @@ namespace System.Web.UI.WebControls {
                protected virtual string FormatDataValue (object value, bool encode)
                {
                        string res;
-                       if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull))
-                               res = NullDisplayText;
+                       if (value == null || (value.ToString ().Length == 0 && ConvertEmptyStringToNull)) {
+                               if (NullDisplayText.Length == 0) {
+                                       encode = false;
+                                       res = " ";
+                               }
+                               else
+                                       res = NullDisplayText;
+                       }
                        else if (DataFormatString.Length > 0)
                                res = string.Format (DataFormatString, value);
                        else
@@ -193,32 +214,38 @@ namespace System.Web.UI.WebControls {
                {
                        return GetBoundValue (Control);
                }
-               
+
                object GetBoundValue (Control controlContainer)
                {
+                       object dataItem = DataBinder.GetDataItem (controlContainer);
+                       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 (DataField == ThisExpression)
-                               return controlContainer.ToString ();
-                       else {
-                               IDataItemContainer dic = (IDataItemContainer) controlContainer;
-                               if (boundProperty == null) {
-                                       boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField];
-                                       if (boundProperty == null)
-                                               new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType());
-                               }
-                               return boundProperty.GetValue (dic.DataItem);
-                       }
+                               return dataItem.ToString ();
+                       else if (DataField == string.Empty)
+                               return null;
+
+                       return DataBinder.GetPropertyValue (dataItem, DataField);
                }
                
                protected virtual void OnDataBindField (object sender, EventArgs e)
                {
-                       DataControlFieldCell cell = (DataControlFieldCell) sender;
+                       Control cell = (Control) sender;
+                       Control controlContainer = cell.BindingContainer;
+                       if (!(controlContainer is INamingContainer))
+                               throw new HttpException ("A DataControlField must be within an INamingContainer.");
+                       object val = GetValue (controlContainer);
+
                        if (cell.Controls.Count > 0) {
                                TextBox box = (TextBox) cell.Controls [0];
-                               object val = GetValue (cell.BindingContainer);
-                               box.Text = val != null ? val.ToString() : "";
+                               if (ApplyFormatInEditMode)
+                                       box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
+                               else
+                                       box.Text = val != null ? val.ToString() : NullDisplayText;
                        }
                        else
-                               cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
+                               ((DataControlFieldCell)cell).Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode);
                }
                
                protected override DataControlField CreateField ()
@@ -237,6 +264,13 @@ namespace System.Web.UI.WebControls {
                        field.ReadOnly = ReadOnly;
                        field.HtmlEncode = HtmlEncode;
                }
+
+               [MonoTODO]
+               public override void ValidateSupportsCallback ()
+               {
+                       throw new NotImplementedException ();
+               }
+
        }
 }
 #endif