2008-03-13 Marek Habersack <mhabersack@novell.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                 [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", false);
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                                 onclick += GetClientScriptEventReference ();
184                         }
185
186                         if (onclick.Length > 0)
187                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
188 #else
189                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
190                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
191                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
192
193                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
194                                 string onclick = Attributes["onclick"];
195                                 if (onclick != null) {
196                                         Attributes.Remove("onclick");
197                                         int len = onclick.Length;
198                                         if (len > 0 && onclick[len - 1] != ';')
199                                                 onclick += ";";
200                                 }
201                                 ClientScriptManager csm = new ClientScriptManager (Page);
202                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
203                                 writer.AddAttribute ("language", "javascript");
204                         }
205 #endif
206
207                         base.AddAttributesToRender (writer);
208                 }
209
210 #if NET_2_0
211                 internal virtual string GetClientScriptEventReference ()
212                 {
213                         PostBackOptions options = GetPostBackOptions ();
214                         return Page.ClientScript.GetPostBackEventReference (options, true);
215                 }
216
217                 protected virtual PostBackOptions GetPostBackOptions () 
218                 {
219                         PostBackOptions options = new PostBackOptions (this);
220                         options.ActionUrl = (PostBackUrl.Length > 0 ? 
221 #if TARGET_J2EE
222                                 CreateActionUrl(PostBackUrl)
223 #else
224                                 Page.ResolveClientUrl (PostBackUrl) 
225 #endif
226                                 : null);
227                         options.ValidationGroup = null;
228                         options.Argument = "";
229                         options.RequiresJavaScriptProtocol = false;
230                         options.ClientSubmit = !UseSubmitBehavior;
231                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
232                         if (options.PerformValidation)
233                                 options.ValidationGroup = ValidationGroup;
234
235                         return options;
236                 }
237 #endif          
238
239                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
240                 {
241                         RaisePostBackEvent (eventArgument);
242                 }
243
244                 protected virtual void OnClick (EventArgs e)
245                 {
246                         if (Events != null) {
247                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
248                                 if (eh != null)
249                                         eh (this, e);
250                         }
251                 }
252
253                 protected virtual void OnCommand (CommandEventArgs e)
254                 {
255                         if (Events != null) {
256                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
257                                 if (eh != null)
258                                         eh (this, e);
259                         }
260
261                         RaiseBubbleEvent (this, e);
262                 }
263
264 #if NET_2_0
265                 protected virtual
266 #endif
267                 void RaisePostBackEvent (string eventArgument)
268                 {
269                         if (CausesValidation)
270 #if NET_2_0
271                                 Page.Validate (ValidationGroup);
272 #else
273                                 Page.Validate ();
274 #endif
275
276                         OnClick (EventArgs.Empty);
277                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
278                 }
279
280 #if NET_2_0
281                 protected internal override void OnPreRender (EventArgs e)
282                 {
283                         // Why override?
284                         base.OnPreRender (e);
285                 }
286 #endif
287                 
288 #if NET_2_0
289                 protected internal
290 #else           
291                 protected
292 #endif
293                 override void RenderContents (HtmlTextWriter writer)
294                 {
295                 }
296                 
297                 [WebSysDescription ("")]
298                 [WebCategory ("Action")]
299                 public event EventHandler Click
300                 {
301                         add {
302                                 Events.AddHandler (ClickEvent, value);
303                         }
304                         remove {
305                                 Events.RemoveHandler (ClickEvent, value);
306                         }
307                 }
308
309                 [WebSysDescription ("")]
310                 [WebCategory ("Action")]
311                 public event CommandEventHandler Command
312                 {
313                         add {
314                                 Events.AddHandler (CommandEvent, value);
315                         }
316                         remove {
317                                 Events.RemoveHandler (CommandEvent, value);
318                         }
319                 }
320
321 #if NET_2_0
322                 [DefaultValue ("")]
323                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
324                 [Themeable (false)]
325                 [UrlProperty("*.aspx")]
326                 public virtual string PostBackUrl {
327                         get {
328                                 return ViewState.GetString ("PostBackUrl", "");
329                         }
330                         set {
331                                 ViewState ["PostBackUrl"] = value;
332                         }
333                 }
334
335                 [DefaultValue ("")]
336                 [Themeable (false)]
337                 [WebSysDescription ("")]
338                 [WebCategoryAttribute ("Behavior")]
339                 public virtual string ValidationGroup {
340                         get {
341                                 return ViewState.GetString ("ValidationGroup", "");
342                         }
343                         set {
344                                 ViewState ["ValidationGroup"] = value;
345                         }
346                 }       
347 #endif
348
349         }
350 }
351
352