Merge pull request #2323 from esdrubal/servicemodel
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ButtonColumn.cs
old mode 100755 (executable)
new mode 100644 (file)
index 5e79a57..94d8ee9
@@ -1,14 +1,11 @@
 //
 // System.Web.UI.WebControls.ButtonColumn.cs
 //
-// Authors:
-//   Gaurav Vaish (gvaish@iitk.ac.in)
-//   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+// Author:
+//      Dick Porter  <dick@ximian.com>
+//      Gonzalo Paniagua Javier (gonzalo@ximian.com)
 //
-// (C) Gaurav Vaish (2002)
-// (C) 2003 Andreas Nahr
-//
-
+// Copyright (C) 2005-2010 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
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-\r
-using System;\r
-using System.ComponentModel;\r
-using System.Web;\r
-using System.Web.UI;\r
-\r
-namespace System.Web.UI.WebControls\r
-{\r
-       public class ButtonColumn : DataGridColumn\r
-       {\r
-               private PropertyDescriptor textFieldDescriptor;\r
-               \r
-               public ButtonColumn(): base()\r
-               {\r
-               }\r
-\r
-               public override void Initialize()\r
-               {\r
-                       base.Initialize();\r
-                       textFieldDescriptor = null;\r
-               }\r
-               \r
-               public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)\r
-               {\r
-                       base.InitializeCell(cell, columnIndex, itemType);\r
-                       if (Enum.IsDefined(typeof(ListItemType), itemType) &&\r
-                           itemType != ListItemType.Footer &&\r
-                           itemType != ListItemType.Header)\r
-                       {\r
-                               WebControl toDisplay = null;\r
-                               if(ButtonType == ButtonColumnType.PushButton)\r
-                               {\r
-                                       Button b = new Button();\r
-                                       b.Text = Text;\r
-                                       b.CommandName = CommandName;\r
-                                       b.CausesValidation = false;\r
-                                       toDisplay = b;\r
-                               } else\r
-                               {\r
-                                       LinkButton lb = new DataGridLinkButton();\r
-                                       lb.Text = Text;\r
-                                       lb.CommandName = CommandName;\r
-                                       lb.CausesValidation = false;\r
-                                       toDisplay = lb;\r
-                               }\r
-                               if(DataTextField.Length > 0)\r
-                               {\r
-                                       toDisplay.DataBinding += new EventHandler(OnDataBindButtonColumn);\r
-                               }\r
-                               cell.Controls.Add(toDisplay);\r
-                       }\r
-               }\r
-               \r
-               private void OnDataBindButtonColumn(object sender, EventArgs e)\r
-               {\r
-                       Control ctrl = (Control)sender;\r
-                       object item = ((DataGridItem)ctrl.NamingContainer).DataItem;\r
-                       if(textFieldDescriptor == null)\r
-                       {\r
-                               textFieldDescriptor = TypeDescriptor.GetProperties(item).Find(DataTextField, true);\r
-                               if(textFieldDescriptor == null && !DesignMode)\r
-                                       throw new HttpException(HttpRuntime.FormatResourceString("Field_Not_Found", DataTextField));\r
-                       }\r
-                       string text;\r
-                       if(textFieldDescriptor != null)\r
-                       {\r
-                               text = FormatDataTextValue(textFieldDescriptor.GetValue(item));\r
-                       } else\r
-                       {\r
-                               text = "Sample_DataBound_Text";\r
-                       }\r
-                       if(ctrl is LinkButton)\r
-                       {\r
-                               ((LinkButton)ctrl).Text = text;\r
-                       }\r
-                       else\r
-                       {\r
-                               ((Button)ctrl).Text = text;\r
-                       }\r
-               }\r
-               \r
-               protected virtual string FormatDataTextValue(object dataTextValue)\r
-               {\r
-                       string retVal = null;\r
-                       if(dataTextValue != null)\r
-                       {\r
-                               if(DataTextFormatString.Length > 0)\r
-                               {\r
-                                       retVal = String.Format(DataTextFormatString, dataTextValue);\r
-                               }\r
-                               else\r
-                               {\r
-                                       retVal = dataTextValue.ToString();\r
-                               }\r
-                       }\r
-                       return retVal;\r
+
+using System.ComponentModel;
+using System.Security.Permissions;
+
+namespace System.Web.UI.WebControls
+{
+       // CAS
+       [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+       public class ButtonColumn : DataGridColumn
+       {
+               string text_field;
+               string format;
+
+               [DefaultValue(ButtonColumnType.LinkButton)]
+               [WebSysDescription("The type of button contained within the column.")]
+               [WebCategory ("Misc")]
+               public virtual ButtonColumnType ButtonType {
+                       get {
+                               return (ButtonColumnType) ViewState.GetInt ("LinkButton",
+                                               (int) ButtonColumnType.LinkButton);
+                       }
+                       set { ViewState ["LinkButton"] = value; }
+               }
+               
+               [DefaultValue("")]
+               [WebSysDescription("The command associated with the button.")]
+               [WebCategory ("Misc")]
+               public virtual string CommandName {
+                       get { return ViewState.GetString ("CommandName", String.Empty); }
+                       set { ViewState ["CommandName"] = value; }
+               }
+
+               [DefaultValue (false)]
+               [WebSysDescription("")]
+               [WebCategory ("Behavior")]
+               public virtual bool CausesValidation {
+                       get { return ViewState.GetBool ("CausesValidation", false); }
+                       set { ViewState ["CausesValidation"] = value; }
+               }
+
+               [DefaultValue("")]
+               [WebSysDescription("The field bound to the text property of the button.")]
+               [WebCategory ("Misc")]
+               public virtual string DataTextField {
+                       get { return ViewState.GetString ("DataTextField", String.Empty); }
+                       set { ViewState ["DataTextField"] = value; }
+               }
+               
+               [DefaultValue("")]
+               [WebSysDescription("The formatting applied to the value bound to the Text property.")]
+               [WebCategory ("Misc")]
+               public virtual string DataTextFormatString {
+                       get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
+                       set {
+                               ViewState ["DataTextFormatString"] = value;
+                               format = null;
+                       }
+               }
+
+               [DefaultValue("")]
+               [Localizable (true)]
+               [WebSysDescription("The text used for the button.")]
+               [WebCategory ("Misc")]
+               public virtual string Text {
+                       get { return ViewState.GetString ("Text", String.Empty); }
+                       set { ViewState ["Text"] = value; }
+               }
+
+               [DefaultValue ("")]
+               [WebSysDescription("")]
+               [WebCategory ("Behavior")]
+               public virtual string ValidationGroup {
+                       get { return ViewState.GetString ("ValidationGroup", String.Empty); }
+                       set { ViewState ["ValidationGroup"] = value; }
+               }
+               
+               public override void Initialize ()
+               {
+                       /* No documentation for this method, so it's
+                        * only here to keep corcompare quiet
+                        */
+                       base.Initialize ();
+               }
+
+               public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
+               {
+                       base.InitializeCell (cell, columnIndex, itemType);
+
+                       if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
+                               switch (ButtonType) {
+                                       case ButtonColumnType.LinkButton: 
+                                       {
+                                               LinkButton butt = new ForeColorLinkButton ();
+                                       
+                                               butt.Text = Text;
+                                               butt.CommandName = CommandName;
+
+                                               if (!String.IsNullOrEmpty (DataTextField))
+                                                       butt.DataBinding += new EventHandler (DoDataBind);
+
+                                               cell.Controls.Add (butt);
+                                       }
+                                       break;
+
+                                       case ButtonColumnType.PushButton: 
+                                       {
+                                               Button butt = new Button ();
+                                       
+                                               butt.Text = Text;
+                                               butt.CommandName = CommandName;
+
+                                               if (!String.IsNullOrEmpty (DataTextField))
+                                                       butt.DataBinding += new EventHandler (DoDataBind);
+                                               cell.Controls.Add (butt);
+                                       }
+                                       break;
+                       
+                               }
+                       }
+               }
+               
+               string GetValueFromItem (DataGridItem item)
+               {
+                       object val = null;
+                       if (text_field == null)
+                               text_field = DataTextField;
+
+                       if (!String.IsNullOrEmpty (text_field))
+                               val = DataBinder.Eval (item.DataItem, text_field);
+
+                       return FormatDataTextValue (val);
+               }
+
+               void DoDataBind (object sender, EventArgs e)
+               {
+                       Control ctrl = (Control) sender;
+                       string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
+
+                       LinkButton lb = sender as LinkButton;
+                       if (lb == null) {
+                               Button b = (Button) sender;
+                               b.Text = text;
+                       } else
+                               lb.Text = text;
+               }
+               
+               protected virtual string FormatDataTextValue (object dataTextValue)
+               {
+                       if (dataTextValue == null)
+                               return String.Empty;
+
+                       if (format == null)
+                               format = DataTextFormatString;
+
+                       if (String.IsNullOrEmpty (format))
+                               return dataTextValue.ToString ();
+
+                       return String.Format (format, dataTextValue);
                }
+       }
+}
 
-               // LAMESPEC The framework uses Description values for metadata here. However they should be WebSysDescriptions
-               // because all metadata in this namespace has WebSysDescriptions\r
-
-               [DefaultValue (typeof (ButtonColumnType), "LinkButton"), WebCategory ("Misc")]
-               [Description ("The type of button used in this column.")]\r
-               public virtual ButtonColumnType ButtonType\r
-               {\r
-                       get\r
-                       {\r
-                               object o = ViewState["ButtonType"];\r
-                               if(o!=null)\r
-                                       return (ButtonColumnType)o;\r
-                               return ButtonColumnType.LinkButton;\r
-                       }\r
-                       set\r
-                       {\r
-                               if(!System.Enum.IsDefined(typeof(ButtonColumnType), value))\r
-                                       throw new ArgumentException();\r
-                               ViewState["ButtonType"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (""), WebCategory ("Misc")]
-               [Description ("The command assigned to this column.")]\r
-               public virtual string CommandName\r
-               {\r
-                       get\r
-                       {\r
-                               string cn = (string)ViewState["CommandName"];\r
-                               if(cn!=null)\r
-                                       return cn;\r
-                               return String.Empty;\r
-                       }\r
-                       set\r
-                       {\r
-                               ViewState["CommandName"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (""), WebCategory ("Misc")]
-               [Description ("The datafield that is bound to the text property.")]\r
-               public virtual string DataTextField\r
-               {\r
-                       get\r
-                       {\r
-                               string dtf = (string)ViewState["DataTextField"];\r
-                               if(dtf!=null)\r
-                                       return dtf;\r
-                               return String.Empty;\r
-                       }\r
-                       set\r
-                       {\r
-                               ViewState["DataTextField"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (""), WebCategory ("Misc")]
-               [Description ("A format that is applied to the bound text property.")]\r
-               public virtual string DataTextFormatString\r
-               {\r
-                       get\r
-                       {\r
-                               string dtfs = (string)ViewState["DataTextFormatString"];\r
-                               if(dtfs!=null)\r
-                                       return dtfs;\r
-                               return String.Empty;\r
-                       }\r
-                       set\r
-                       {\r
-                               ViewState["DataTextFormatString"] = value;\r
-                       }\r
-               }\r
-
-               [DefaultValue (""), WebCategory ("Misc")]
-               [Description ("The text used for this button.")]\r
-               public virtual string Text\r
-               {\r
-                       get\r
-                       {\r
-                               string text = (string)ViewState["Text"];\r
-                               if(text!=null)\r
-                                       return text;\r
-                               return String.Empty;\r
-                       }\r
-                       set\r
-                       {\r
-                               ViewState["Text"] = value;\r
-                       }\r
-               }\r
-       }\r
-}\r