Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CommandField.cs
1 //
2 // System.Web.UI.WebControls.CommandField.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.Collections;
32 using System.Collections.Specialized;
33 using System.Web.UI;
34 using System.ComponentModel;
35 using System.Security.Permissions;
36
37 namespace System.Web.UI.WebControls
38 {
39         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public class CommandField : ButtonFieldBase
42         {
43                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
44                 [UrlPropertyAttribute]
45                 [DefaultValueAttribute ("")]
46                 [WebSysDescription ("")]
47                 [WebCategoryAttribute ("Appearance")]
48                 public virtual string CancelImageUrl {
49                         get { return ViewState.GetString ("CancelImageUrl", String.Empty); }
50                         set {
51                                 ViewState ["CancelImageUrl"] = value;
52                                 OnFieldChanged ();
53                         }
54                 }
55
56                 [LocalizableAttribute (true)]
57                 [WebSysDescription ("")]
58                 [WebCategoryAttribute ("Appearance")]
59                 public virtual string CancelText {
60                         get { return ViewState.GetString ("CancelText", "Cancel"); }
61                         set {
62                                 ViewState ["CancelText"] = value;
63                                 OnFieldChanged ();
64                         }
65                 }
66
67                 [DefaultValueAttribute (true)]
68                 public override bool CausesValidation {
69                         get { return ViewState.GetBool ("CausesValidation", true); }
70                         set {
71                                 ViewState ["CausesValidation"] = value;
72                                 OnFieldChanged ();
73                         }
74                 }
75
76                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
77                 [UrlPropertyAttribute]
78                 [DefaultValueAttribute ("")]
79                 [WebSysDescription ("")]
80                 [WebCategoryAttribute ("Appearance")]
81                 public virtual string DeleteImageUrl {
82                         get { return ViewState.GetString ("DeleteImageUrl", String.Empty); }
83                         set {
84                                 ViewState ["DeleteImageUrl"] = value;
85                                 OnFieldChanged ();
86                         }
87                 }
88
89                 [LocalizableAttribute (true)]
90                 [WebSysDescription ("")]
91                 [WebCategoryAttribute ("Appearance")]
92                 public virtual string DeleteText {
93                         get { return ViewState.GetString ("DeleteText", "Delete"); }
94                         set {
95                                 ViewState ["DeleteText"] = value;
96                                 OnFieldChanged ();
97                         }
98                 }
99
100                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
101                 [UrlPropertyAttribute]
102                 [DefaultValueAttribute ("")]
103                 [WebSysDescription ("")]
104                 [WebCategoryAttribute ("Appearance")]
105                 public virtual string EditImageUrl {
106                         get { return ViewState.GetString ("EditImageUrl", String.Empty); }
107                         set {
108                                 ViewState ["EditImageUrl"] = value;
109                                 OnFieldChanged ();
110                         }
111                 }
112
113                 [LocalizableAttribute (true)]
114                 [WebSysDescription ("")]
115                 [WebCategoryAttribute ("Appearance")]
116                 public virtual string EditText {
117                         get { return ViewState.GetString ("EditText", "Edit"); }
118                         set {
119                                 ViewState ["EditText"] = value;
120                                 OnFieldChanged ();
121                         }
122                 }
123
124                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
125                 [UrlPropertyAttribute]
126                 [DefaultValueAttribute ("")]
127                 [WebSysDescription ("")]
128                 [WebCategoryAttribute ("Appearance")]
129                 public virtual string InsertImageUrl {
130                         get { return ViewState.GetString ("InsertImageUrl", String.Empty); }
131                         set {
132                                 ViewState ["InsertImageUrl"] = value;
133                                 OnFieldChanged ();
134                         }
135                 }
136
137                 [LocalizableAttribute (true)]
138                 [WebSysDescription ("")]
139                 [WebCategoryAttribute ("Appearance")]
140                 public virtual string InsertText {
141                         get { return ViewState.GetString ("InsertText", "Insert"); }
142                         set {
143                                 ViewState ["InsertText"] = value;
144                                 OnFieldChanged ();
145                         }
146                 }
147
148                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
149                 [UrlPropertyAttribute]
150                 [DefaultValueAttribute ("")]
151                 [WebSysDescription ("")]
152                 [WebCategoryAttribute ("Appearance")]
153                 public virtual string NewImageUrl {
154                         get { return ViewState.GetString ("NewImageUrl", String.Empty); }
155                         set {
156                                 ViewState ["NewImageUrl"] = value;
157                                 OnFieldChanged ();
158                         }
159                 }
160
161                 [LocalizableAttribute (true)]
162                 [WebSysDescription ("")]
163                 [WebCategoryAttribute ("Appearance")]
164                 public virtual string NewText {
165                         get { return ViewState.GetString ("NewText", "New"); }
166                         set {
167                                 ViewState ["NewText"] = value;
168                                 OnFieldChanged ();
169                         }
170                 }
171
172                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
173                 [UrlPropertyAttribute]
174                 [DefaultValueAttribute ("")]
175                 [WebSysDescription ("")]
176                 [WebCategoryAttribute ("Appearance")]
177                 public virtual string SelectImageUrl {
178                         get { return ViewState.GetString ("SelectImageUrl", String.Empty); }
179                         set {
180                                 ViewState ["SelectImageUrl"] = value;
181                                 OnFieldChanged ();
182                         }
183                 }
184
185                 [LocalizableAttribute (true)]
186                 [WebSysDescription ("")]
187                 [WebCategoryAttribute ("Appearance")]
188                 public virtual string SelectText {
189                         get { return ViewState.GetString ("SelectText", "Select"); }
190                         set {
191                                 ViewState ["SelectText"] = value;
192                                 OnFieldChanged ();
193                         }
194                 }
195                 
196                 [DefaultValueAttribute (true)]
197                 [WebSysDescription ("")]
198                 [WebCategoryAttribute ("Behavior")]
199                 public virtual bool ShowCancelButton {
200                         get { return ViewState.GetBool ("ShowCancelButton", true); }
201                         set {
202                                 ViewState ["ShowCancelButton"] = value;
203                                 OnFieldChanged ();
204                         }
205                 }
206                 
207                 [DefaultValueAttribute (false)]
208                 [WebSysDescription ("")]
209                 [WebCategoryAttribute ("Behavior")]
210                 public virtual bool ShowDeleteButton {
211                         get { return ViewState.GetBool ("ShowDeleteButton", false); }
212                         set {
213                                 ViewState ["ShowDeleteButton"] = value;
214                                 OnFieldChanged ();
215                         }
216                 }
217                 
218                 [DefaultValueAttribute (false)]
219                 [WebSysDescription ("")]
220                 [WebCategoryAttribute ("Behavior")]
221                 public virtual bool ShowEditButton {
222                         get { return ViewState.GetBool ("ShowEditButton", false); }
223                         set {
224                                 ViewState ["ShowEditButton"] = value;
225                                 OnFieldChanged ();
226                         }
227                 }
228                 
229                 [DefaultValueAttribute (false)]
230                 [WebSysDescription ("")]
231                 [WebCategoryAttribute ("Behavior")]
232                 public virtual bool ShowSelectButton {
233                         get { return ViewState.GetBool ("ShowSelectButton", false); }
234                         set {
235                                 ViewState ["ShowSelectButton"] = value;
236                                 OnFieldChanged ();
237                         }
238                 }
239                 
240                 [DefaultValueAttribute (false)]
241                 [WebSysDescription ("")]
242                 [WebCategoryAttribute ("Behavior")]
243                 public virtual bool ShowInsertButton {
244                         get { return ViewState.GetBool ("ShowInsertButton", false); }
245                         set {
246                                 ViewState ["ShowInsertButton"] = value;
247                                 OnFieldChanged ();
248                         }
249                 }
250
251                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
252                 [UrlPropertyAttribute]
253                 [DefaultValueAttribute ("")]
254                 [WebSysDescription ("")]
255                 [WebCategoryAttribute ("Appearance")]
256                 public virtual string UpdateImageUrl {
257                         get { return ViewState.GetString ("UpdateImageUrl", String.Empty); }
258                         set {
259                                 ViewState ["UpdateImageUrl"] = value;
260                                 OnFieldChanged ();
261                         }
262                 }
263
264                 [LocalizableAttribute (true)]
265                 [WebSysDescription ("")]
266                 [WebCategoryAttribute ("Appearance")]
267                 public virtual string UpdateText {
268                         get { return ViewState.GetString ("UpdateText", "Update"); }
269                         set {
270                                 ViewState ["UpdateText"] = value;
271                                 OnFieldChanged ();
272                         }
273                 }
274                 
275                 public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
276                 {
277                         string index = rowIndex.ToString ();
278                         
279                         if (cellType == DataControlCellType.DataCell) {
280                                 if ((rowState & DataControlRowState.Edit) != 0) {
281                                         if (ShowEditButton) {
282                                                 cell.Controls.Add (CreateButton (UpdateText, UpdateImageUrl, DataControlCommands.UpdateCommandName, index));
283                                                 if (ShowCancelButton) {
284                                                         AddSeparator (cell);
285                                                         cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
286                                                 }
287                                         }
288                                 } else if ((rowState & DataControlRowState.Insert) != 0) {
289                                         if (ShowInsertButton) {
290                                                 cell.Controls.Add (CreateButton (InsertText, InsertImageUrl, DataControlCommands.InsertCommandName, index));
291                                                 if (ShowCancelButton) {
292                                                         AddSeparator (cell);
293                                                         cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));
294                                                 }
295                                         }
296                                 } else {
297                                         if (ShowEditButton) {
298                                                 AddSeparator (cell);
299                                                 cell.Controls.Add (CreateButton (EditText, EditImageUrl, DataControlCommands.EditCommandName, index));
300                                         }
301                                         if (ShowDeleteButton) {
302                                                 AddSeparator (cell);
303                                                 cell.Controls.Add (CreateButton (DeleteText, DeleteImageUrl, DataControlCommands.DeleteCommandName, index));
304                                         }
305                                         if (ShowInsertButton) {
306                                                 AddSeparator (cell);
307                                                 cell.Controls.Add (CreateButton (NewText, NewImageUrl, DataControlCommands.NewCommandName, index));
308                                         }
309                                         if (ShowSelectButton) {
310                                                 AddSeparator (cell);
311                                                 cell.Controls.Add (CreateButton (SelectText, SelectImageUrl, DataControlCommands.SelectCommandName, index));
312                                         }
313                                 }
314                         } else
315                                 base.InitializeCell (cell, cellType, rowState, rowIndex);
316                 }
317                 
318                 Control CreateButton (string text, string image, string command, string arg)
319                 {
320                         IDataControlButton c = DataControlButton.CreateButton (ButtonType, Control, text, image, command, arg, false);
321                         if (CausesValidation) {
322                                 if (command == DataControlCommands.UpdateCommandName || command == DataControlCommands.InsertCommandName) {
323                                         c.Container = null;
324                                         c.CausesValidation = true;
325                                         c.ValidationGroup = ValidationGroup;
326                                 }
327                         }
328                         return (Control)c;
329                 }
330                 
331                 void AddSeparator (DataControlFieldCell cell)
332                 {
333                         if (cell.Controls.Count > 0) {
334                                 Literal lit = new Literal ();
335                                 lit.Text = " ";
336                                 cell.Controls.Add (lit);
337                         }
338                 }
339                 
340                 protected override DataControlField CreateField ()
341                 {
342                         return new CommandField ();
343                 }
344                 
345                 protected override void CopyProperties (DataControlField newField)
346                 {
347                         base.CopyProperties (newField);
348                         CommandField field = (CommandField) newField;
349                         field.CancelImageUrl = CancelImageUrl;
350                         field.CancelText = CancelText;
351                         field.DeleteImageUrl = DeleteImageUrl;
352                         field.DeleteText = DeleteText;
353                         field.EditImageUrl = EditImageUrl;
354                         field.EditText = EditText;
355                         field.InsertImageUrl = InsertImageUrl;
356                         field.InsertText = InsertText;
357                         field.NewImageUrl = NewImageUrl;
358                         field.NewText = NewText;
359                         field.SelectImageUrl = SelectImageUrl;
360                         field.SelectText = SelectText;
361                         field.ShowCancelButton = ShowCancelButton;
362                         field.ShowDeleteButton = ShowDeleteButton;
363                         field.ShowEditButton = ShowEditButton;
364                         field.ShowSelectButton = ShowSelectButton;
365                         field.ShowInsertButton = ShowInsertButton;
366                         field.UpdateImageUrl = UpdateImageUrl;
367                         field.UpdateText = UpdateText;
368                 }
369                 
370                 public override void ValidateSupportsCallback ()
371                 {
372                         if (ShowSelectButton)
373                                 throw new NotSupportedException ("Callbacks are not supported on CommandField when the select button is enabled because other controls on your page that are dependent on the selected value of '" + Control.ID + "' for their rendering will not update in a callback.  Turn callbacks off on '" + Control.ID + "'.");
374                 }
375         }
376 }
377