2006-06-14 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 #endif          
93                 public string CommandArgument {
94                         get {
95                                 return ViewState.GetString ("CommandArgument", "");
96                         }
97                         set {
98                                 ViewState ["CommandArgument"] = value;
99                         }
100                 }
101
102                 [DefaultValue ("")]
103                 [WebSysDescription ("")]
104                 [WebCategory ("Behavior")]
105 #if NET_2_0
106                 [Themeable (false)]
107 #endif          
108                 public string CommandName {
109                         get {
110                                 return ViewState.GetString ("CommandName", "");
111                         }
112                         set {
113                                 ViewState ["CommandName"] = value;
114                         }
115                 }
116
117 #if NET_2_0
118                 [Themeable (false)]
119                 [DefaultValue ("")]
120                 [MonoTODO]
121                 [WebSysDescription ("")]
122                 [WebCategoryAttribute ("Behavior")]
123                 public virtual string OnClientClick 
124                 {
125                         get {
126                                 throw new NotImplementedException ();
127                         }
128                         set {
129                                 throw new NotImplementedException ();
130                         }
131                 }
132
133 #endif          
134
135                 [DefaultValue ("")]
136                 [Bindable (true)]
137                 [WebSysDescription ("")]
138                 [WebCategory ("Appearance")]
139 #if NET_2_0
140                 [Localizable (true)]
141 #endif          
142                 public string Text {
143                         get {
144                                 return ViewState.GetString ("Text", "");
145                         }
146                         set {
147                                 ViewState ["Text"] = value;
148                         }
149                 }
150
151 #if NET_2_0
152                 [DefaultValue (true)]
153                 [Themeable (false)]
154                 [MonoTODO]
155                 [WebSysDescription ("")]
156                 [WebCategoryAttribute ("Behavior")]
157                 public virtual bool UseSubmitBehavior 
158                 {
159                         get {
160                                 throw new NotImplementedException ();
161                         }
162                         set {
163                                 throw new NotImplementedException ();
164                         }
165                 }
166 #endif          
167
168                 protected override void AddAttributesToRender (HtmlTextWriter writer)
169                 {
170                         if (Page != null)
171                                 Page.VerifyRenderingInServerForm (this);
172
173                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
174 #if NET_2_0
175                         if (ID != null)
176                                 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
177 #else
178                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
179 #endif
180                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
181
182                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
183                                 string onclick = Attributes["onclick"];
184                                 if (onclick != null) {
185                                         Attributes.Remove("onclick");
186                                         int len = onclick.Length;
187                                         if (len > 0 && onclick[len - 1] != ';')
188                                                 onclick += ";";
189                                 }
190                                 ClientScriptManager csm = new ClientScriptManager (Page);
191                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
192                                 writer.AddAttribute ("language", "javascript");
193                         }
194
195                         base.AddAttributesToRender (writer);
196                 }
197
198 #if NET_2_0
199                 [MonoTODO]
200                 protected virtual PostBackOptions GetPostBackOptions ()
201                 {
202                         throw new NotImplementedException ();
203                 }
204 #endif          
205
206                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
207                 {
208                         RaisePostBackEvent (eventArgument);
209                 }
210
211                 protected virtual void OnClick (EventArgs e)
212                 {
213                         if (Events != null) {
214                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
215                                 if (eh != null)
216                                         eh (this, e);
217                         }
218                 }
219
220                 protected virtual void OnCommand (CommandEventArgs e)
221                 {
222                         if (Events != null) {
223                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
224                                 if (eh != null)
225                                         eh (this, e);
226                         }
227
228                         RaiseBubbleEvent (this, e);
229                 }
230
231 #if NET_2_0
232                 protected virtual
233 #endif
234                 void RaisePostBackEvent (string eventArgument)
235                 {
236                         if (CausesValidation)
237 #if NET_2_0
238                                 Page.Validate (ValidationGroup);
239 #else
240                                 Page.Validate ();
241 #endif
242
243                         OnClick (EventArgs.Empty);
244                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
245                 }
246
247 #if NET_2_0
248                 protected internal
249 #else           
250                 protected
251 #endif          
252                 override void RenderContents (HtmlTextWriter writer)
253                 {
254                 }
255
256                 [WebSysDescription ("")]
257                 [WebCategory ("Action")]
258                 public event EventHandler Click
259                 {
260                         add {
261                                 Events.AddHandler (ClickEvent, value);
262                         }
263                         remove {
264                                 Events.RemoveHandler (ClickEvent, value);
265                         }
266                 }
267
268                 [WebSysDescription ("")]
269                 [WebCategory ("Action")]
270                 public event CommandEventHandler Command
271                 {
272                         add {
273                                 Events.AddHandler (CommandEvent, value);
274                         }
275                         remove {
276                                 Events.RemoveHandler (CommandEvent, value);
277                         }
278                 }
279
280 #if NET_2_0
281                 [DefaultValue ("")]
282                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
283                 [Themeable (false)]
284                 [UrlProperty("*.aspx")]
285                 [MonoTODO]
286                 public virtual string PostBackUrl {
287                         get {
288                                 return ViewState.GetString ("PostBackUrl", "");
289                         }
290                         set {
291                                 ViewState ["PostBackUrl"] = value;
292                         }
293                 }
294
295                 [DefaultValue ("")]
296                 [Themeable (false)]
297                 [WebSysDescription ("")]
298                 [WebCategoryAttribute ("Behavior")]
299                 public virtual string ValidationGroup {
300                         get {
301                                 return ViewState.GetString ("ValidationGroup", "");
302                         }
303                         set {
304                                 ViewState ["ValidationGroup"] = value;
305                         }
306                 }       
307 #endif
308
309         }
310 }
311