New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ButtonColumn.cs
1 //
2 // System.Web.UI.WebControls.ButtonColumn.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.ComponentModel;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         public class ButtonColumn : DataGridColumn {
39                        
40                 [DefaultValue(ButtonColumnType.LinkButton)]
41 #if NET_2_0
42                 [WebSysDescription("The type of button contained within the column.")]
43 #else
44                 [Description("The type of button contained within the column.")]
45 #endif
46                 [WebCategory ("Misc")]
47                 public virtual ButtonColumnType ButtonType
48                 {
49                         get {
50                                 return (ButtonColumnType) ViewState.GetInt ("LinkButton",
51                                                 (int) ButtonColumnType.LinkButton);
52                         }
53                         set {
54                                 ViewState ["LinkButton"] = value;
55                         }
56                 }
57                 
58                 [DefaultValue("")]
59 #if NET_2_0
60                 [WebSysDescription("The command associated with the button.")]
61 #else
62                 [Description("The command associated with the button.")]
63 #endif
64                 [WebCategory ("Misc")]
65                 public virtual string CommandName
66                 {
67                         get {
68                                 return ViewState.GetString ("CommandName", String.Empty);
69                         }
70                         set {
71                                 ViewState ["CommandName"] = value;
72                         }
73                 }
74
75 #if NET_2_0
76                 [DefaultValue (false)]
77                 [WebSysDescription("")]
78                 [WebCategory ("Behavior")]
79                 public virtual bool CausesValidation
80                 {
81                         get {
82                                 return ViewState.GetBool ("CausesValidation", false);
83                         }
84                         set {
85                                 ViewState ["CausesValidation"] = value;
86                         }
87                 }
88 #endif
89
90                 [DefaultValue("")]
91 #if NET_2_0
92                 [WebSysDescription("The field bound to the text property of the button.")]
93 #else
94                 [Description("The field bound to the text property of the button.")]
95 #endif
96                 [WebCategory ("Misc")]
97                 public virtual string DataTextField 
98                 {
99                         get {
100                                 return ViewState.GetString ("DataTextField", String.Empty);
101                         }
102                         set {
103                                 ViewState ["DataTextField"] = value;
104                         }
105                 }
106                 
107                 [DefaultValue("")]
108 #if NET_2_0
109                 [WebSysDescription("The formatting applied to the value bound to the Text property.")]
110 #else
111                 [Description("The formatting applied to the value bound to the Text property.")]
112 #endif
113                 [WebCategory ("Misc")]
114                 public virtual string DataTextFormatString 
115                 {
116                         get {
117                                 return ViewState.GetString ("DataTextFormatString",
118                                                 String.Empty);
119                         }
120                         set {
121                                 ViewState ["DataTextFormatString"] = value;
122                                 format = null;
123                         }
124                 }
125
126                 [DefaultValue("")]
127 #if NET_2_0
128                 [Localizable (true)]
129                 [WebSysDescription("The text used for the button.")]
130 #else
131                 [Description("The text used for the button.")]
132 #endif
133                 [WebCategory ("Misc")]
134                 public virtual string Text 
135                 {
136                         get {
137                                 return ViewState.GetString ("Text", String.Empty);
138                         }
139                         set {
140                                 ViewState ["Text"] = value;
141                         }
142                 }
143
144 #if NET_2_0
145                 [DefaultValue ("")]
146                 [WebSysDescription("")]
147                 [WebCategory ("Behavior")]
148                 public virtual string ValidationGroup
149                 {
150                         get {
151                                 return ViewState.GetString ("ValidationGroup", "");
152                         }
153                         set {
154                                 ViewState ["ValidationGroup"] = value;
155                         }
156                 }
157 #endif
158                 
159                 public override void Initialize ()
160                 {
161                         /* No documentation for this method, so it's
162                          * only here to keep corcompare quiet
163                          */
164                         base.Initialize ();
165                 }
166
167                 public override void InitializeCell (TableCell cell,
168                                                      int columnIndex,
169                                                      ListItemType itemType)
170                 {
171                         base.InitializeCell (cell, columnIndex, itemType);
172
173                         if (itemType != ListItemType.Header &&
174                             itemType != ListItemType.Footer) {
175                                 switch (ButtonType) {
176                                 case ButtonColumnType.LinkButton: 
177                                 {
178                                         LinkButton butt = new ForeColorLinkButton ();
179                                         
180                                         butt.Text = Text;
181                                         butt.CommandName = CommandName;
182
183                                         if (DataTextField != "")
184                                                 butt.DataBinding += new EventHandler (DoDataBind);
185
186                                         cell.Controls.Add (butt);
187                                 }
188                                 break;
189
190                                 case ButtonColumnType.PushButton: 
191                                 {
192                                         Button butt = new Button ();
193                                         
194                                         butt.Text = Text;
195                                         butt.CommandName = CommandName;
196
197                                         if (DataTextField != "")
198                                                 butt.DataBinding += new EventHandler (DoDataBind);
199                                         cell.Controls.Add (butt);
200                                 }
201                                 break;
202                         
203                                 }
204                         }
205                 }
206
207                 string text_field;
208                 string GetValueFromItem (DataGridItem item)
209                 {
210                         object val = null;
211                         if (text_field == null)
212                                 text_field = DataTextField;
213
214                         if (text_field != "")
215                                 val = DataBinder.Eval (item.DataItem, text_field);
216
217                         return FormatDataTextValue (val);
218                 }
219
220                 void DoDataBind (object sender, EventArgs e)
221                 {
222                         Control ctrl = (Control) sender;
223                         string text = GetValueFromItem ((DataGridItem) ctrl.NamingContainer);
224
225                         LinkButton lb = sender as LinkButton;
226                         if (lb == null) {
227                                 Button b = (Button) sender;
228                                 b.Text = text;
229                         } else {
230                                 lb.Text = text;
231                         }
232                 }
233
234                 string format;
235                 protected virtual string FormatDataTextValue (object dataTextValue)
236                 {
237                         if (dataTextValue == null)
238                                 return "";
239
240                         if (format == null)
241                                 format = DataTextFormatString;
242
243                         if (format == "")
244                                 return dataTextValue.ToString ();
245
246                         return String.Format (format, dataTextValue);
247                 }
248         }
249 }
250