Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ButtonField.cs
1 //
2 // System.Web.UI.WebControls.ButtonField.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Reflection;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Web.UI;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37
38 namespace System.Web.UI.WebControls
39 {
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public class ButtonField : ButtonFieldBase
43         {
44                 PropertyDescriptor boundProperty;
45
46                 [DefaultValueAttribute ("")]
47                 [WebSysDescription ("Raised when a Button Command is executed.")]
48                 [WebCategoryAttribute ("Behavior")]
49                 public virtual string CommandName {
50                         get { return ViewState.GetString ("CommandName", String.Empty); }
51                         set {
52                                 ViewState ["CommandName"] = value;
53                                 OnFieldChanged ();
54                         }
55                 }
56                 
57                 [DefaultValueAttribute ("")]
58                 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
59                 [WebSysDescription ("")]
60                 [WebCategoryAttribute ("Data")]
61                 public virtual string DataTextField {
62                         get { return ViewState.GetString ("DataTextField", String.Empty); }
63                         set {
64                                 ViewState ["DataTextField"] = value;
65                                 OnFieldChanged ();
66                         }
67                 }
68                 
69                 [DefaultValueAttribute ("")]
70                 [WebSysDescription ("")]
71                 [WebCategoryAttribute ("Data")]
72                 public virtual string DataTextFormatString {
73                         get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
74                         set {
75                                 ViewState ["DataTextFormatString"] = value;
76                                 OnFieldChanged ();
77                         }
78                 }
79                 
80                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
81                 [WebSysDescription ("")]
82                 [WebCategoryAttribute ("Appearance")]
83                 [DefaultValueAttribute ("")]
84                 [UrlPropertyAttribute]
85                 public virtual string ImageUrl {
86                         get { return ViewState.GetString ("ImageUrl", String.Empty); }
87                         set {
88                                 ViewState ["ImageUrl"] = value;
89                                 OnFieldChanged ();
90                         }
91                 }
92                 
93                 [LocalizableAttribute (true)]
94                 [DefaultValueAttribute ("")]
95                 [WebSysDescription ("")]
96                 [WebCategoryAttribute ("Appearance")]
97                 public virtual string Text {
98                         get { return ViewState.GetString ("Text", String.Empty); }
99                         set {
100                                 ViewState ["Text"] = value;
101                                 OnFieldChanged ();
102                         }
103                 }
104                 
105                 public override bool Initialize (bool sortingEnabled, Control control)
106                 {
107                         return base.Initialize (sortingEnabled, control);
108                 }
109                 
110                 protected virtual string FormatDataTextValue (object dataTextValue)
111                 {
112                         if (DataTextFormatString.Length > 0)
113                                 return String.Format (DataTextFormatString, dataTextValue);
114                         else if (dataTextValue == null)
115                                 return String.Empty;
116                         else
117                                 return dataTextValue.ToString ();
118                 }
119                 
120                 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
121                 {
122                         string index = rowIndex.ToString ();
123                         
124                         if (cellType == DataControlCellType.DataCell) {
125                                 
126                                 IDataControlButton btn = DataControlButton.CreateButton (ButtonType, Control, Text, ImageUrl, CommandName, index, false);
127
128                                 if (CausesValidation) {
129                                         btn.Container = null;
130                                         btn.CausesValidation = true;
131                                         btn.ValidationGroup = ValidationGroup;
132                                 }
133                                 
134                                 if (!String.IsNullOrEmpty (DataTextField)) {
135                                         if ((rowState & DataControlRowState.Insert) == 0)
136                                                 cell.DataBinding += new EventHandler (OnDataBindField);
137                                 }
138                                 cell.Controls.Add ((Control) btn);
139                         } else
140                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
141                 }
142                 
143                 void OnDataBindField (object sender, EventArgs e)
144                 {
145                         DataControlFieldCell cell = (DataControlFieldCell) sender;
146                         IDataControlButton btn = (IDataControlButton) cell.Controls [0]; 
147                         btn.Text = FormatDataTextValue (GetBoundValue (cell.BindingContainer));
148                 }
149                 
150                 object GetBoundValue (Control controlContainer)
151                 {
152                         IDataItemContainer dic = controlContainer as IDataItemContainer;
153                         if (boundProperty == null) {
154                                 boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataTextField];
155                                 if (boundProperty == null)
156                                         throw new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + dic.DataItem.GetType());
157                         }
158                         return boundProperty.GetValue (dic.DataItem);
159                 }
160                 
161                 protected override DataControlField CreateField ()
162                 {
163                         return new ButtonField ();
164                 }
165                 
166                 protected override void CopyProperties (DataControlField newField)
167                 {
168                         base.CopyProperties (newField);
169                         ButtonField field = (ButtonField) newField;
170                         field.CommandName = CommandName;
171                         field.DataTextField = DataTextField;
172                         field.DataTextFormatString = DataTextFormatString;
173                         field.ImageUrl = ImageUrl;
174                         field.Text = Text;
175                 }
176
177                 // MSDN: The ValidateSupportsCallback method is a helper method used to determine 
178                 // whether the controls contained in a BoundField object support callbacks. 
179                 // This method has been implemented as an empty method (a method that does not 
180                 // contain any code) to indicate that callbacks are supported.
181                 public override void ValidateSupportsCallback ()
182                 {
183                 }
184         }
185 }