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