Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataControlField.cs
index 4ea6ea1a92515e7e9e0f9eb64e6d7156437c4ab3..902d75b3ca717d06d62b0c0f299ae5846373a940 100644 (file)
@@ -5,7 +5,7 @@
 //     Sanjay Gupta (gsanjay@novell.com)
 //     Lluis Sanchez Gual (lluis@novell.com)
 //
-// (C) 2004 Novell, Inc. (http://www.novell.com)
+// (C) 2004-2010 Novell, Inc. (http://www.novell.com)
 //
 
 //
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-
-#if NET_2_0
 using System.Collections;
 using System.Collections.Specialized;
 using System.Web.UI;
 using System.ComponentModel;
 using System.Security.Permissions;
 
-namespace System.Web.UI.WebControls {
-
+namespace System.Web.UI.WebControls
+{
        [DefaultPropertyAttribute ("HeaderText")]
        [TypeConverterAttribute (typeof(ExpandableObjectConverter))]
        [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        public abstract class DataControlField : IStateManager, IDataSourceViewSchemaAccessor
        {
+               static readonly object fieldChangedEvent = new object ();
+               
                bool tracking = false;
                StateBag viewState;
                Control control;
@@ -52,6 +52,12 @@ namespace System.Web.UI.WebControls {
                TableItemStyle headerStyle;
                TableItemStyle itemStyle;
                bool sortingEnabled;
+               EventHandlerList events = new EventHandlerList ();
+               
+               internal event EventHandler FieldChanged {
+                       add { events.AddHandler (fieldChangedEvent, value); }
+                       remove { events.RemoveHandler (fieldChangedEvent, value); }
+               }
                
                protected DataControlField()
                { 
@@ -60,7 +66,7 @@ namespace System.Web.UI.WebControls {
                
                internal void SetDirty ()
                {
-                       viewState.SetDirty ();
+                       viewState.SetDirty (true);
                }
                
                protected StateBag ViewState {
@@ -76,23 +82,24 @@ namespace System.Web.UI.WebControls {
                {
                        this.sortingEnabled = sortingEnabled;
                        this.control = control;
-                       return true;
+                       return false;
                }
 
                public virtual void InitializeCell (DataControlFieldCell cell,
                        DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
                {
-                       if (cellType == DataControlCellType.Header)
-                       {
-                               if (HeaderText.Length > 0 || HeaderImageUrl.Length > 0) {
-                                       if (sortingEnabled && SortExpression.Length > 0)
-                                               cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, SortExpression, true));
-                                       else
-                                               cell.Controls.Add (new DataControlButton (control, HeaderText, HeaderImageUrl, string.Empty, string.Empty, true));
-                               }
-                       }
-                       else if (cellType == DataControlCellType.Footer) {
-                               cell.Text = FooterText;
+                       if (cellType == DataControlCellType.Header) {
+                               if (HeaderText.Length > 0 && sortingEnabled && SortExpression.Length > 0)
+                                       cell.Controls.Add ((Control) DataControlButton.CreateButton (String.IsNullOrEmpty (HeaderImageUrl) ? ButtonType.Link : ButtonType.Image, control, HeaderText, HeaderImageUrl, DataControlCommands.SortCommandName, SortExpression, true));
+                               else if (HeaderImageUrl.Length > 0) {
+                                       Image image = new Image ();
+                                       image.ImageUrl = HeaderImageUrl;
+                                       cell.Controls.Add (image);
+                               } else
+                                       cell.Text = HeaderText.Length > 0 ? HeaderText : " ";
+                       } else if (cellType == DataControlCellType.Footer) {
+                               string footerText = FooterText;
+                               cell.Text = (footerText.Length > 0) ? footerText : " ";
                        }
                }
                
@@ -123,8 +130,10 @@ namespace System.Web.UI.WebControls {
                
                protected virtual void OnFieldChanged ()
                {
-                       if (FieldChanged != null)
-                               FieldChanged (this, EventArgs.Empty);
+                       EventHandler eh = events [fieldChangedEvent] as EventHandler;
+                       
+                       if (eh != null)
+                               eh (this, EventArgs.Empty);
                }       
        
                protected virtual void LoadViewState (object savedState)
@@ -136,16 +145,16 @@ namespace System.Web.UI.WebControls {
                        viewState.LoadViewState (states[0]);
                        
                        if (states[1] != null)
-                               ((IStateManager)controlStyle).LoadViewState (states[1]);
+                               ((IStateManager)ControlStyle).LoadViewState (states[1]);
                        if (states[2] != null)
-                               ((IStateManager)footerStyle).LoadViewState (states[2]);
+                               ((IStateManager)FooterStyle).LoadViewState (states[2]);
                        if (states[3] != null)
-                               ((IStateManager)headerStyle).LoadViewState (states[3]);
+                               ((IStateManager)HeaderStyle).LoadViewState (states[3]);
                        if (states[4] != null)
-                               ((IStateManager)itemStyle).LoadViewState (states[4]);
+                               ((IStateManager)ItemStyle).LoadViewState (states[4]);
                }
 
-               protected virtual object SaveViewState()
+               protected virtual object SaveViewState ()
                {
                        object[] state = new object [5];
                        state [0] = viewState.SaveViewState ();
@@ -165,7 +174,7 @@ namespace System.Web.UI.WebControls {
                        return state;
                }
 
-               protected virtual void TrackViewState()
+               protected virtual void TrackViewState ()
                {
                        if (controlStyle != null) ((IStateManager) controlStyle).TrackViewState ();
                        if (footerStyle != null) ((IStateManager) footerStyle).TrackViewState ();
@@ -180,19 +189,19 @@ namespace System.Web.UI.WebControls {
                        throw new NotSupportedException ("Callback not supported");
                }
 
-               void IStateManager.LoadViewState(object savedState)
+               void IStateManager.LoadViewState (object savedState)
                {
-                       LoadViewState(savedState);
+                       LoadViewState (savedState);
                }
 
-               object IStateManager.SaveViewState()
+               object IStateManager.SaveViewState ()
                {
-                       return SaveViewState();
+                       return SaveViewState ();
                }
 
-               void IStateManager.TrackViewState()
+               void IStateManager.TrackViewState ()
                {
-                       TrackViewState();
+                       TrackViewState ();
                }
                
                internal Exception GetNotSupportedPropException (string propName)
@@ -200,6 +209,14 @@ namespace System.Web.UI.WebControls {
                        return new System.NotSupportedException ("The property '" + propName + "' is not supported in " + GetType().Name); 
                }
 
+               internal bool ControlStyleCreated { get { return controlStyle != null; } }
+               
+               internal bool HeaderStyleCreated { get { return headerStyle != null; } }
+               
+               internal bool FooterStyleCreated { get { return footerStyle != null; } }
+               
+               internal bool ItemStyleCreated { get { return itemStyle != null; } }
+
                [MonoTODO ("Render this")]
                [DefaultValueAttribute ("")]
                [LocalizableAttribute (true)]
@@ -207,7 +224,7 @@ namespace System.Web.UI.WebControls {
                public virtual string AccessibleHeaderText {
                        get {
                                object val = viewState ["accessibleHeaderText"];
-                               return val != null ? (string) val : "";
+                               return val != null ? (string) val : String.Empty;
                        }
                        set { 
                                viewState ["accessibleHeaderText"] = value;
@@ -223,7 +240,7 @@ namespace System.Web.UI.WebControls {
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
                [DefaultValueAttribute (null)]
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
-               public virtual Style ControlStyle {
+               public Style ControlStyle {
                        get {
                                if (controlStyle == null) {
                                        controlStyle = new Style ();
@@ -242,7 +259,7 @@ namespace System.Web.UI.WebControls {
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
                [WebCategoryAttribute ("Styles")]
-               public virtual TableItemStyle FooterStyle {
+               public TableItemStyle FooterStyle {
                        get {
                                if (footerStyle == null) {
                                        footerStyle = new TableItemStyle ();
@@ -259,7 +276,7 @@ namespace System.Web.UI.WebControls {
                public virtual string FooterText {
                        get {
                                object val = viewState ["footerText"];
-                               return val != null ? (string) val : "";
+                               return val != null ? (string) val : String.Empty;
                        }
                        set { 
                                viewState ["footerText"] = value;
@@ -274,7 +291,7 @@ namespace System.Web.UI.WebControls {
                public virtual string HeaderImageUrl {
                        get {
                                object val = viewState ["headerImageUrl"];
-                               return val != null ? (string) val : "";
+                               return val != null ? (string) val : String.Empty;
                        }
                        set { 
                                viewState ["headerImageUrl"] = value;
@@ -286,7 +303,7 @@ namespace System.Web.UI.WebControls {
                [WebCategoryAttribute ("Styles")]
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
                [DefaultValueAttribute (null)]
-               public virtual TableItemStyle HeaderStyle {
+               public TableItemStyle HeaderStyle {
                        get {
                                if (headerStyle == null) {
                                        headerStyle = new TableItemStyle ();
@@ -303,7 +320,7 @@ namespace System.Web.UI.WebControls {
                public virtual string HeaderText {
                        get {
                                object val = viewState ["headerText"];
-                               return val != null ? (string) val : "";
+                               return val != null ? (string) val : String.Empty;
                        }
                        set { 
                                viewState ["headerText"] = value;
@@ -328,7 +345,7 @@ namespace System.Web.UI.WebControls {
                [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
                [WebCategoryAttribute ("Styles")]
                [DefaultValueAttribute (null)]
-               public virtual TableItemStyle ItemStyle {
+               public TableItemStyle ItemStyle {
                        get {
                                if (itemStyle == null) {
                                        itemStyle = new TableItemStyle ();
@@ -358,7 +375,7 @@ namespace System.Web.UI.WebControls {
                public virtual string SortExpression {
                        get {
                                object val = viewState ["sortExpression"];
-                               return val != null ? (string) val : "";
+                               return val != null ? (string) val : String.Empty;
                        }
                        set { 
                                viewState ["sortExpression"] = value;
@@ -374,6 +391,8 @@ namespace System.Web.UI.WebControls {
                                return val != null ? (bool) val : true;
                        }
                        set { 
+                               if (value == Visible)
+                                       return;
                                viewState ["visible"] = value;
                                OnFieldChanged ();
                        }
@@ -394,9 +413,13 @@ namespace System.Web.UI.WebControls {
                        set { 
                                viewState ["dataSourceViewSchema"] = value;
                        }
-               }               
+               }
 
-               internal event EventHandler FieldChanged;
+               public override string ToString ()
+               {
+                       if (string.IsNullOrEmpty (HeaderText))
+                               return base.ToString ();
+                       return HeaderText;
+               }
        }
 }
-#endif