merge from trunk revisions 58933, 58935, 58936
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Button.cs
1 //
2 // System.Web.UI.WebControls.Button.cs
3 //
4 // Authors:
5 //      Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.ComponentModel.Design;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [DefaultEvent ("Click")]
40         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41         [DefaultProperty ("Text")]
42         [Designer ("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43
44 #if NET_2_0
45         [ToolboxDataAttribute ("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
46         [SupportsEventValidation]
47 #else
48         [ToolboxDataAttribute ("<{0}:Button runat=server Text=\"Button\"></{0}:Button>")]
49 #endif
50
51         public class Button : WebControl, IPostBackEventHandler
52 #if NET_2_0
53         , IButtonControl
54 #endif
55         {
56
57                 private static readonly object ClickEvent = new object ();
58                 private static readonly object CommandEvent = new object ();
59
60                 public Button () : base (HtmlTextWriterTag.Input)
61                 {
62                 }
63
64 #if ONLY_1_1
65                 [Bindable (false)]
66 #endif          
67                 [WebSysDescription ("")]
68                 [WebCategory ("Behavior")]
69                 [DefaultValue (true)]
70 #if NET_2_0
71                 [Themeable (false)]
72                 public virtual
73 #else           
74                 public
75 #endif          
76                 bool CausesValidation {
77                         get {
78                                 return ViewState.GetBool ("CausesValidation", true);
79                         }
80
81                         set {
82                                 ViewState ["CausesValidation"] = value;
83                         }
84                 }
85
86                 [DefaultValue ("")]
87                 [Bindable (true)]
88                 [WebSysDescription ("")]
89                 [WebCategory ("Behavior")]
90 #if NET_2_0
91                 [Themeable (false)]
92                 public virtual
93 #else           
94                 public
95 #endif          
96                 string CommandArgument {
97                         get {
98                                 return ViewState.GetString ("CommandArgument", "");
99                         }
100                         set {
101                                 ViewState ["CommandArgument"] = value;
102                         }
103                 }
104
105                 [DefaultValue ("")]
106                 [WebSysDescription ("")]
107                 [WebCategory ("Behavior")]
108 #if NET_2_0
109                 [Themeable (false)]
110                 public virtual
111 #else
112                 public
113 #endif          
114                 string CommandName {
115                         get {
116                                 return ViewState.GetString ("CommandName", "");
117                         }
118                         set {
119                                 ViewState ["CommandName"] = value;
120                         }
121                 }
122
123 #if NET_2_0
124                 [Themeable (false)]
125                 [DefaultValue ("")]
126                 [MonoTODO]
127                 [WebSysDescription ("")]
128                 [WebCategoryAttribute ("Behavior")]
129                 public virtual string OnClientClick 
130                 {
131                         get {
132                                 throw new NotImplementedException ();
133                         }
134                         set {
135                                 throw new NotImplementedException ();
136                         }
137                 }
138
139 #endif          
140
141                 [DefaultValue ("")]
142                 [Bindable (true)]
143                 [WebSysDescription ("")]
144                 [WebCategory ("Appearance")]
145 #if NET_2_0
146                 [Localizable (true)]
147                 public virtual
148 #else           
149                 public
150 #endif          
151                 string Text {
152                         get {
153                                 return ViewState.GetString ("Text", "");
154                         }
155                         set {
156                                 ViewState ["Text"] = value;
157                         }
158                 }
159
160 #if NET_2_0
161                 [DefaultValue (true)]
162                 [Themeable (false)]
163                 [MonoTODO]
164                 [WebSysDescription ("")]
165                 [WebCategoryAttribute ("Behavior")]
166                 public virtual bool UseSubmitBehavior 
167                 {
168                         get {
169                                 throw new NotImplementedException ();
170                         }
171                         set {
172                                 throw new NotImplementedException ();
173                         }
174                 }
175 #endif          
176
177                 protected override void AddAttributesToRender (HtmlTextWriter writer)
178                 {
179                         if (Page != null)
180                                 Page.VerifyRenderingInServerForm (this);
181
182                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
183 #if NET_2_0
184                         if (ID != null)
185                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
186 #else
187                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
188 #endif
189                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
190
191                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
192                                 ClientScriptManager csm = new ClientScriptManager (Page);
193                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
194                                 writer.AddAttribute ("language", "javascript");
195                         }
196
197                         base.AddAttributesToRender (writer);
198                 }
199
200 #if NET_2_0
201                 [MonoTODO]
202                 protected virtual PostBackOptions GetPostBackOptions ()
203                 {
204                         throw new NotImplementedException ();
205                 }
206 #endif          
207
208                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
209                 {
210                         RaisePostBackEvent (eventArgument);
211                 }
212
213                 protected virtual void OnClick (EventArgs e)
214                 {
215                         if (Events != null) {
216                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
217                                 if (eh != null)
218                                         eh (this, e);
219                         }
220                 }
221
222                 protected virtual void OnCommand (CommandEventArgs e)
223                 {
224                         if (Events != null) {
225                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
226                                 if (eh != null)
227                                         eh (this, e);
228                         }
229
230                         RaiseBubbleEvent (this, e);
231                 }
232
233 #if NET_2_0
234                 protected virtual
235 #endif
236                 void RaisePostBackEvent (string eventArgument)
237                 {
238                         if (CausesValidation)
239 #if NET_2_0
240                                 Page.Validate (ValidationGroup);
241 #else
242                                 Page.Validate ();
243 #endif
244
245                         OnClick (EventArgs.Empty);
246                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
247                 }
248
249 #if NET_2_0
250                 protected internal
251 #else           
252                 protected
253 #endif          
254                 override void RenderContents (HtmlTextWriter writer)
255                 {
256                 }
257
258                 [WebSysDescription ("")]
259                 [WebCategory ("Action")]
260                 public event EventHandler Click
261                 {
262                         add {
263                                 Events.AddHandler (ClickEvent, value);
264                         }
265                         remove {
266                                 Events.RemoveHandler (ClickEvent, value);
267                         }
268                 }
269
270                 [WebSysDescription ("")]
271                 [WebCategory ("Action")]
272                 public event CommandEventHandler Command
273                 {
274                         add {
275                                 Events.AddHandler (CommandEvent, value);
276                         }
277                         remove {
278                                 Events.RemoveHandler (CommandEvent, value);
279                         }
280                 }
281
282 #if NET_2_0
283                 [DefaultValue ("")]
284                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
285                 [Themeable (false)]
286                 [UrlProperty("*.aspx")]
287                 [MonoTODO]
288                 public string PostBackUrl {
289                         get {
290                                 throw new NotImplementedException ();
291                         }
292                         set {
293                                 throw new NotImplementedException ();
294                         }
295                 }
296
297                 [DefaultValue ("")]
298                 [Themeable (false)]
299                 [WebSysDescription ("")]
300                 [WebCategoryAttribute ("Behavior")]
301                 public string ValidationGroup {
302                         get {
303                                 return ViewState.GetString ("ValidationGroup", "");
304                         }
305                         set {
306                                 ViewState ["ValidationGroup"] = value;
307                         }
308                 }       
309 #endif
310
311         }
312 }
313