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