Add semicolon after client script if does not exist
[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                                 string onclick = Attributes["onclick"];
193                                 if (onclick != null) {
194                                         Attributes.Remove("onclick");
195                                         if (!onclick.Trim().EndsWith(";"))
196                                                 onclick += ";";
197                                 }
198                                 ClientScriptManager csm = new ClientScriptManager (Page);
199                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
200                                 writer.AddAttribute ("language", "javascript");
201                         }
202
203                         base.AddAttributesToRender (writer);
204                 }
205
206 #if NET_2_0
207                 [MonoTODO]
208                 protected virtual PostBackOptions GetPostBackOptions ()
209                 {
210                         throw new NotImplementedException ();
211                 }
212 #endif          
213
214                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
215                 {
216                         RaisePostBackEvent (eventArgument);
217                 }
218
219                 protected virtual void OnClick (EventArgs e)
220                 {
221                         if (Events != null) {
222                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
223                                 if (eh != null)
224                                         eh (this, e);
225                         }
226                 }
227
228                 protected virtual void OnCommand (CommandEventArgs e)
229                 {
230                         if (Events != null) {
231                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
232                                 if (eh != null)
233                                         eh (this, e);
234                         }
235
236                         RaiseBubbleEvent (this, e);
237                 }
238
239 #if NET_2_0
240                 protected virtual
241 #endif
242                 void RaisePostBackEvent (string eventArgument)
243                 {
244                         if (CausesValidation)
245 #if NET_2_0
246                                 Page.Validate (ValidationGroup);
247 #else
248                                 Page.Validate ();
249 #endif
250
251                         OnClick (EventArgs.Empty);
252                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
253                 }
254
255 #if NET_2_0
256                 protected internal
257 #else           
258                 protected
259 #endif          
260                 override void RenderContents (HtmlTextWriter writer)
261                 {
262                 }
263
264                 [WebSysDescription ("")]
265                 [WebCategory ("Action")]
266                 public event EventHandler Click
267                 {
268                         add {
269                                 Events.AddHandler (ClickEvent, value);
270                         }
271                         remove {
272                                 Events.RemoveHandler (ClickEvent, value);
273                         }
274                 }
275
276                 [WebSysDescription ("")]
277                 [WebCategory ("Action")]
278                 public event CommandEventHandler Command
279                 {
280                         add {
281                                 Events.AddHandler (CommandEvent, value);
282                         }
283                         remove {
284                                 Events.RemoveHandler (CommandEvent, value);
285                         }
286                 }
287
288 #if NET_2_0
289                 [DefaultValue ("")]
290                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
291                 [Themeable (false)]
292                 [UrlProperty("*.aspx")]
293                 [MonoTODO]
294                 public string PostBackUrl {
295                         get {
296                                 throw new NotImplementedException ();
297                         }
298                         set {
299                                 throw new NotImplementedException ();
300                         }
301                 }
302
303                 [DefaultValue ("")]
304                 [Themeable (false)]
305                 [WebSysDescription ("")]
306                 [WebCategoryAttribute ("Behavior")]
307                 public string ValidationGroup {
308                         get {
309                                 return ViewState.GetString ("ValidationGroup", "");
310                         }
311                         set {
312                                 ViewState ["ValidationGroup"] = value;
313                         }
314                 }       
315 #endif
316
317         }
318 }
319