In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CommandField.cs
1 //\r
2 // System.Web.UI.WebControls.CommandField.cs\r
3 //\r
4 // Authors:\r
5 //      Lluis Sanchez Gual (lluis@novell.com)\r
6 //\r
7 // (C) 2005 Novell, Inc (http://www.novell.com)\r
8 //\r
9 \r
10 //\r
11 // Permission is hereby granted, free of charge, to any person obtaining\r
12 // a copy of this software and associated documentation files (the\r
13 // "Software"), to deal in the Software without restriction, including\r
14 // without limitation the rights to use, copy, modify, merge, publish,\r
15 // distribute, sublicense, and/or sell copies of the Software, and to\r
16 // permit persons to whom the Software is furnished to do so, subject to\r
17 // the following conditions:\r
18 // \r
19 // The above copyright notice and this permission notice shall be\r
20 // included in all copies or substantial portions of the Software.\r
21 // \r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
29 //\r
30 \r
31 #if NET_2_0\r
32 using System.Collections;\r
33 using System.Collections.Specialized;\r
34 using System.Web.UI;\r
35 using System.ComponentModel;\r
36 using System.Security.Permissions;\r
37 \r
38 namespace System.Web.UI.WebControls {\r
39 \r
40         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]\r
41         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]\r
42         public class CommandField : ButtonFieldBase\r
43         {\r
44                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
45                 [UrlPropertyAttribute]\r
46                 [DefaultValueAttribute ("")]\r
47                 [WebSysDescription ("")]\r
48                 [WebCategoryAttribute ("Appearance")]\r
49                 public virtual string CancelImageUrl {\r
50                         get { return ViewState.GetString ("CancelImageUrl", ""); }\r
51                         set {\r
52                                 ViewState ["CancelImageUrl"] = value;\r
53                                 OnFieldChanged ();\r
54                         }\r
55                 }\r
56 \r
57                 [LocalizableAttribute (true)]\r
58                 [WebSysDescription ("")]\r
59                 [WebCategoryAttribute ("Appearance")]\r
60                 public virtual string CancelText {\r
61                         get { return ViewState.GetString ("CancelText", "Cancel"); }\r
62                         set {\r
63                                 ViewState ["CancelText"] = value;\r
64                                 OnFieldChanged ();\r
65                         }\r
66                 }\r
67 \r
68                 [DefaultValueAttribute (true)]\r
69                 public override bool CausesValidation {\r
70                         get { return ViewState.GetBool ("CausesValidation", true); }\r
71                         set {\r
72                                 ViewState ["CausesValidation"] = value;\r
73                                 OnFieldChanged ();\r
74                         }\r
75                 }\r
76 \r
77                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
78                 [UrlPropertyAttribute]\r
79                 [DefaultValueAttribute ("")]\r
80                 [WebSysDescription ("")]\r
81                 [WebCategoryAttribute ("Appearance")]\r
82                 public virtual string DeleteImageUrl {\r
83                         get { return ViewState.GetString ("DeleteImageUrl", ""); }\r
84                         set {\r
85                                 ViewState ["DeleteImageUrl"] = value;\r
86                                 OnFieldChanged ();\r
87                         }\r
88                 }\r
89 \r
90                 [LocalizableAttribute (true)]\r
91                 [WebSysDescription ("")]\r
92                 [WebCategoryAttribute ("Appearance")]\r
93                 public virtual string DeleteText {\r
94                         get { return ViewState.GetString ("DeleteText", "Delete"); }\r
95                         set {\r
96                                 ViewState ["DeleteText"] = value;\r
97                                 OnFieldChanged ();\r
98                         }\r
99                 }\r
100 \r
101                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
102                 [UrlPropertyAttribute]\r
103                 [DefaultValueAttribute ("")]\r
104                 [WebSysDescription ("")]\r
105                 [WebCategoryAttribute ("Appearance")]\r
106                 public virtual string EditImageUrl {\r
107                         get { return ViewState.GetString ("EditImageUrl", ""); }\r
108                         set {\r
109                                 ViewState ["EditImageUrl"] = value;\r
110                                 OnFieldChanged ();\r
111                         }\r
112                 }\r
113 \r
114                 [LocalizableAttribute (true)]\r
115                 [WebSysDescription ("")]\r
116                 [WebCategoryAttribute ("Appearance")]\r
117                 public virtual string EditText {\r
118                         get { return ViewState.GetString ("EditText", "Edit"); }\r
119                         set {\r
120                                 ViewState ["EditText"] = value;\r
121                                 OnFieldChanged ();\r
122                         }\r
123                 }\r
124 \r
125                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
126                 [UrlPropertyAttribute]\r
127                 [DefaultValueAttribute ("")]\r
128                 [WebSysDescription ("")]\r
129                 [WebCategoryAttribute ("Appearance")]\r
130                 public virtual string InsertImageUrl {\r
131                         get { return ViewState.GetString ("InsertImageUrl", ""); }\r
132                         set {\r
133                                 ViewState ["InsertImageUrl"] = value;\r
134                                 OnFieldChanged ();\r
135                         }\r
136                 }\r
137 \r
138                 [LocalizableAttribute (true)]\r
139                 [WebSysDescription ("")]\r
140                 [WebCategoryAttribute ("Appearance")]\r
141                 public virtual string InsertText {\r
142                         get { return ViewState.GetString ("InsertText", "Insert"); }\r
143                         set {\r
144                                 ViewState ["InsertText"] = value;\r
145                                 OnFieldChanged ();\r
146                         }\r
147                 }\r
148 \r
149                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
150                 [UrlPropertyAttribute]\r
151                 [DefaultValueAttribute ("")]\r
152                 [WebSysDescription ("")]\r
153                 [WebCategoryAttribute ("Appearance")]\r
154                 public virtual string NewImageUrl {\r
155                         get { return ViewState.GetString ("NewImageUrl", ""); }\r
156                         set {\r
157                                 ViewState ["NewImageUrl"] = value;\r
158                                 OnFieldChanged ();\r
159                         }\r
160                 }\r
161 \r
162                 [LocalizableAttribute (true)]\r
163                 [WebSysDescription ("")]\r
164                 [WebCategoryAttribute ("Appearance")]\r
165                 public virtual string NewText {\r
166                         get { return ViewState.GetString ("NewText", "New"); }\r
167                         set {\r
168                                 ViewState ["NewText"] = value;\r
169                                 OnFieldChanged ();\r
170                         }\r
171                 }\r
172 \r
173                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
174                 [UrlPropertyAttribute]\r
175                 [DefaultValueAttribute ("")]\r
176                 [WebSysDescription ("")]\r
177                 [WebCategoryAttribute ("Appearance")]\r
178                 public virtual string SelectImageUrl {\r
179                         get { return ViewState.GetString ("SelectImageUrl", ""); }\r
180                         set {\r
181                                 ViewState ["SelectImageUrl"] = value;\r
182                                 OnFieldChanged ();\r
183                         }\r
184                 }\r
185 \r
186                 [LocalizableAttribute (true)]\r
187                 [WebSysDescription ("")]\r
188                 [WebCategoryAttribute ("Appearance")]\r
189                 public virtual string SelectText {\r
190                         get { return ViewState.GetString ("SelectText", "Select"); }\r
191                         set {\r
192                                 ViewState ["SelectText"] = value;\r
193                                 OnFieldChanged ();\r
194                         }\r
195                 }\r
196                 \r
197                 [DefaultValueAttribute (true)]\r
198                 [WebSysDescription ("")]\r
199                 [WebCategoryAttribute ("Behavior")]\r
200                 public virtual bool ShowCancelButton {\r
201                         get { return ViewState.GetBool ("ShowCancelButton", true); }\r
202                         set {\r
203                                 ViewState ["ShowCancelButton"] = value;\r
204                                 OnFieldChanged ();\r
205                         }\r
206                 }\r
207                 \r
208                 [DefaultValueAttribute (false)]\r
209                 [WebSysDescription ("")]\r
210                 [WebCategoryAttribute ("Behavior")]\r
211                 public virtual bool ShowDeleteButton {\r
212                         get { return ViewState.GetBool ("ShowDeleteButton", false); }\r
213                         set {\r
214                                 ViewState ["ShowDeleteButton"] = value;\r
215                                 OnFieldChanged ();\r
216                         }\r
217                 }\r
218                 \r
219                 [DefaultValueAttribute (false)]\r
220                 [WebSysDescription ("")]\r
221                 [WebCategoryAttribute ("Behavior")]\r
222                 public virtual bool ShowEditButton {\r
223                         get { return ViewState.GetBool ("ShowEditButton", false); }\r
224                         set {\r
225                                 ViewState ["ShowEditButton"] = value;\r
226                                 OnFieldChanged ();\r
227                         }\r
228                 }\r
229                 \r
230                 [DefaultValueAttribute (false)]\r
231                 [WebSysDescription ("")]\r
232                 [WebCategoryAttribute ("Behavior")]\r
233                 public virtual bool ShowSelectButton {\r
234                         get { return ViewState.GetBool ("ShowSelectButton", false); }\r
235                         set {\r
236                                 ViewState ["ShowSelectButton"] = value;\r
237                                 OnFieldChanged ();\r
238                         }\r
239                 }\r
240                 \r
241                 [DefaultValueAttribute (false)]\r
242                 [WebSysDescription ("")]\r
243                 [WebCategoryAttribute ("Behavior")]\r
244                 public virtual bool ShowInsertButton {\r
245                         get { return ViewState.GetBool ("ShowInsertButton", false); }\r
246                         set {\r
247                                 ViewState ["ShowInsertButton"] = value;\r
248                                 OnFieldChanged ();\r
249                         }\r
250                 }\r
251 \r
252                 [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]\r
253                 [UrlPropertyAttribute]\r
254                 [DefaultValueAttribute ("")]\r
255                 [WebSysDescription ("")]\r
256                 [WebCategoryAttribute ("Appearance")]\r
257                 public virtual string UpdateImageUrl {\r
258                         get { return ViewState.GetString ("UpdateImageUrl", ""); }\r
259                         set {\r
260                                 ViewState ["UpdateImageUrl"] = value;\r
261                                 OnFieldChanged ();\r
262                         }\r
263                 }\r
264 \r
265                 [LocalizableAttribute (true)]\r
266                 [WebSysDescription ("")]\r
267                 [WebCategoryAttribute ("Appearance")]\r
268                 public virtual string UpdateText {\r
269                         get { return ViewState.GetString ("UpdateText", "Update"); }\r
270                         set {\r
271                                 ViewState ["UpdateText"] = value;\r
272                                 OnFieldChanged ();\r
273                         }\r
274                 }\r
275                 \r
276                 public override void InitializeCell (DataControlFieldCell cell,\r
277                         DataControlCellType cellType, DataControlRowState rowState, int rowIndex)\r
278                 {\r
279                         string index = rowIndex.ToString ();\r
280                         \r
281                         if (cellType == DataControlCellType.DataCell)\r
282                         {\r
283                                 if ((rowState & DataControlRowState.Edit) != 0) {\r
284                                         if (ShowEditButton) {\r
285                                                 cell.Controls.Add (CreateButton (UpdateText, UpdateImageUrl, DataControlCommands.UpdateCommandName, index));\r
286                                                 if (ShowCancelButton) {\r
287                                                         AddSeparator (cell);\r
288                                                         cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));\r
289                                                 }\r
290                                         }\r
291                                 } else if ((rowState & DataControlRowState.Insert) != 0) {\r
292                                         if (ShowInsertButton) {\r
293                                                 cell.Controls.Add (CreateButton (InsertText, InsertImageUrl, DataControlCommands.InsertCommandName, index));\r
294                                                 if (ShowCancelButton) {\r
295                                                         AddSeparator (cell);\r
296                                                         cell.Controls.Add (CreateButton (CancelText, CancelImageUrl, DataControlCommands.CancelCommandName, index));\r
297                                                 }\r
298                                         }\r
299                                 } else {\r
300                                         if (ShowEditButton) {\r
301                                                 AddSeparator (cell);\r
302                                                 cell.Controls.Add (CreateButton (EditText, EditImageUrl, DataControlCommands.EditCommandName, index));\r
303                                         }\r
304                                         if (ShowDeleteButton) {\r
305                                                 AddSeparator (cell);\r
306                                                 cell.Controls.Add (CreateButton (DeleteText, DeleteImageUrl, DataControlCommands.DeleteCommandName, index));\r
307                                         }\r
308                                         if (ShowInsertButton) {\r
309                                                 AddSeparator (cell);\r
310                                                 cell.Controls.Add (CreateButton (NewText, NewImageUrl, DataControlCommands.NewCommandName, index));\r
311                                         }\r
312                                         if (ShowSelectButton) {\r
313                                                 AddSeparator (cell);\r
314                                                 cell.Controls.Add (CreateButton (SelectText, SelectImageUrl, DataControlCommands.SelectCommandName, index));\r
315                                         }\r
316                                 }\r
317                         } else\r
318                                 base.InitializeCell (cell, cellType, rowState, rowIndex);\r
319                 }\r
320                 \r
321                 Control CreateButton (string text, string image, string command, string arg)\r
322                 {\r
323                         IDataControlButton c = DataControlButton.CreateButton (ButtonType, Control, text, image, command, arg, false);\r
324                         if (CausesValidation) {\r
325                                 if (command == DataControlCommands.UpdateCommandName || command == DataControlCommands.InsertCommandName) {\r
326                                         c.Container = null;\r
327                                         c.CausesValidation = true;\r
328                                         c.ValidationGroup = ValidationGroup;\r
329                                 }\r
330                         }\r
331                         return (Control)c;\r
332                 }\r
333                 \r
334                 void AddSeparator (DataControlFieldCell cell)\r
335                 {\r
336                         if (cell.Controls.Count > 0) {\r
337                                 Literal lit = new Literal ();\r
338                                 lit.Text = " ";\r
339                                 cell.Controls.Add (lit);\r
340                         }\r
341                 }\r
342                 \r
343                 protected override DataControlField CreateField ()\r
344                 {\r
345                         return new CommandField ();\r
346                 }\r
347                 \r
348                 protected override void CopyProperties (DataControlField newField)\r
349                 {\r
350                         base.CopyProperties (newField);\r
351                         CommandField field = (CommandField) newField;\r
352                         field.CancelImageUrl = CancelImageUrl;\r
353                         field.CancelText = CancelText;\r
354                         field.DeleteImageUrl = DeleteImageUrl;\r
355                         field.DeleteText = DeleteText;\r
356                         field.EditImageUrl = EditImageUrl;\r
357                         field.EditText = EditText;\r
358                         field.InsertImageUrl = InsertImageUrl;\r
359                         field.InsertText = InsertText;\r
360                         field.NewImageUrl = NewImageUrl;\r
361                         field.NewText = NewText;\r
362                         field.SelectImageUrl = SelectImageUrl;\r
363                         field.SelectText = SelectText;\r
364                         field.ShowCancelButton = ShowCancelButton;\r
365                         field.ShowDeleteButton = ShowDeleteButton;\r
366                         field.ShowEditButton = ShowEditButton;\r
367                         field.ShowSelectButton = ShowSelectButton;\r
368                         field.ShowInsertButton = ShowInsertButton;\r
369                         field.UpdateImageUrl = UpdateImageUrl;\r
370                         field.UpdateText = UpdateText;\r
371                 }\r
372                 \r
373                 public override void ValidateSupportsCallback ()\r
374                 {\r
375                         if (ShowSelectButton)\r
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 + "'.");\r
377                 }\r
378         }\r
379 }\r
380 #endif\r