2005-02-04 Lluis Sanchez Gual <lluis@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 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //\r
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36 using System.Web;
37 using System.Web.UI;
38
39 namespace System.Web.UI.WebControls
40 {
41         [DefaultEvent("Click")]
42         [DefaultProperty("Text")]
43         [Designer("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
44         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
45         [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
46         public class Button : WebControl, IPostBackEventHandler
47 #if NET_2_0
48                 , IButtonControl
49 #endif
50         {
51                 private static readonly object ClickEvent   = new object();
52                 private static readonly object CommandEvent = new object();
53
54                 //private EventHandlerList ehList;
55
56                 public Button(): base (HtmlTextWriterTag.Input)
57                 {
58                 }
59
60 #if NET_2_0
61         [ThemeableAttribute (false)]
62 #else
63                 [Bindable (false)]
64 #endif
65                 [DefaultValue (true), WebCategory ("Behavior")]
66                 [WebSysDescription ("Determines if validation is performed when clicked.")]
67                 public bool CausesValidation
68                 {
69                         get
70                         {
71                                 Object cv = ViewState["CausesValidation"];
72                                 if(cv!=null)
73                                         return (Boolean)cv;
74                                 return true;
75                         }
76                         set
77                         {
78                                 ViewState["CausesValidation"] = value;
79                         }
80                 }
81
82 #if NET_2_0
83         [ThemeableAttribute (false)]
84 #endif
85                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
86                 [WebSysDescription ("An argument for the Command of this control.")]
87                 public string CommandArgument
88                 {
89                         get
90                         {
91                                 string ca = (string) ViewState["CommandArgument"];
92                                 if(ca!=null)
93                                         return ca;
94                                 return String.Empty;
95                         }
96                         set
97                         {
98                                 ViewState["CommandArgument"] = value;
99                         }
100                 }
101
102 #if NET_2_0
103         [ThemeableAttribute (false)]
104 #endif
105                 [DefaultValue (""), WebCategory ("Behavior")]
106                 [WebSysDescription ("The name of the Command of this control.")]
107                 public string CommandName
108                 {
109                         get
110                         {
111                                 string cn = (string)ViewState["CommandName"];
112                                 if(cn!=null)
113                                         return cn;
114                                 return String.Empty;
115                         }
116                         set
117                         {
118                                 ViewState["CommandName"] = value;
119                         }
120                 }
121
122 #if NET_2_0
123         [Localizable (true)]
124 #endif
125                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
126                 [WebSysDescription ("The text that should be shown on this Button.")]
127                 public string Text
128                 {
129                         get
130                         {
131                                 string text = (string)ViewState["Text"];
132                                 if(text!=null)
133                                         return text;
134                                 return String.Empty;
135                         }
136                         set
137                         {
138                                 ViewState["Text"] = value;
139                         }
140                 }
141                 
142 #if NET_2_0
143                 [ThemeableAttribute (false)]
144                 [EditorAttribute ("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
145                 [DefaultValueAttribute ("")]
146                 [UrlProperty]
147                 public string PostBackUrl {
148                         get {
149                                 string text = (string)ViewState["PostBackUrl"];
150                                 if (text!=null) return text;
151                                 return String.Empty;
152                         }
153                         set {
154                                 ViewState["PostBackUrl"] = value;
155                         }
156                 }
157                 
158                 [BindableAttribute (true)]
159                 [LocalizableAttribute (true)]
160                 [DefaultValueAttribute ("")]
161                 public string SoftkeyLabel {
162                         get {
163                                 string text = (string)ViewState["SoftkeyLabel"];
164                                 if (text!=null) return text;
165                                 return String.Empty;
166                         }
167                         set {
168                                 ViewState["SoftkeyLabel"] = value;
169                         }
170                 }
171                 
172                 [DefaultValueAttribute ("")]
173                 [ThemeableAttribute (false)]
174                 [WebCategoryAttribute ("Behavior")]
175                 public string ValidationGroup {
176                         get {
177                                 string text = (string)ViewState["ValidationGroup"];
178                                 if (text!=null) return text;
179                                 return String.Empty;
180                         }
181                         set {
182                                 ViewState["ValidationGroup"] = value;
183                         }
184                 }
185                 
186                 [DefaultValueAttribute ("")]
187                 [ThemeableAttribute (false)]
188                 [WebCategoryAttribute ("Behavior")]
189                 public string OnClientClick {
190                         get {
191                                 string text = (string)ViewState["OnClientClick"];
192                                 if (text!=null) return text;
193                                 return String.Empty;
194                         }
195                         set {
196                                 ViewState["OnClientClick"] = value;
197                         }
198                 }
199                 
200                 [DefaultValueAttribute (true)]
201                 [ThemeableAttribute (false)]
202                 [WebCategoryAttribute ("Behavior")]
203                 public bool UseSubmitBehavior {
204                         get {
205                                 object b = ViewState["UseSubmitBehavior"];
206                                 if (b != null) return (bool)b;
207                                 return true;
208                         }
209                         set {
210                                 ViewState["UseSubmitBehavior"] = value;
211                         }
212                 }
213 #endif
214
215                 [WebCategory ("Action")]
216                 [WebSysDescription ("Raised when the Button is clicked.")]
217                 public event EventHandler Click
218                 {
219                         add
220                         {
221                                 Events.AddHandler(ClickEvent, value);
222                         }
223                         remove
224                         {
225                                 Events.RemoveHandler(ClickEvent, value);
226                         }
227                 }
228
229                 [WebCategory ("Action")]
230                 [WebSysDescription ("Raised when a Button Command is executed.")]
231                 public event CommandEventHandler Command
232                 {
233                         add
234                         {
235                                 Events.AddHandler(CommandEvent, value);
236                         }
237                         remove
238                         {
239                                 Events.RemoveHandler(CommandEvent, value);
240                         }
241                 }
242
243                 protected virtual void OnClick(EventArgs e)
244                 {
245                         if(Events != null)
246                         {
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                         {
257                                 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
258                                 if(eh!= null)
259                                         eh(this,e);
260                         }
261                         RaiseBubbleEvent(this, e);
262                 }
263
264 #if NET_2_0
265                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
266                 {
267                         RaisePostBackEvent (eventArgument);
268                 }
269                 
270                 protected virtual void RaisePostBackEvent (string eventArgument)
271 #else
272                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
273 #endif
274                 {
275                         if (CausesValidation)
276                                 Page.Validate ();
277
278                         OnClick (EventArgs.Empty);
279                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
280                 }
281                 
282 #if NET_2_0
283                 protected virtual PostBackOptions GetPostBackOptions ()
284                 {
285                         PostBackOptions ops = new PostBackOptions (this);
286                         if (PostBackUrl != "")
287                                 ops.ActionUrl = PostBackUrl;
288                         ops.PerformValidation = Page.Validators.Count > 0 && CausesValidation;
289                         if (ops.PerformValidation)
290                                 ops.ValidationGroup = ValidationGroup;
291                         ops.ClientSubmit = UseSubmitBehavior;
292                         ops.RequiresJavaScriptProtocol = false;
293                         return ops;
294                 }
295 #endif
296
297                 protected override void AddAttributesToRender (HtmlTextWriter writer)
298                 {
299                         if (Page != null)
300                                 Page.VerifyRenderingInServerForm (this);
301
302                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
303                         writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
304                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
305
306                         if (Page != null) {
307                                 string script = "";
308 #if NET_2_0
309                                 script = OnClientClick;
310                                 if (script.Length > 0) script += ";";
311                                 
312                                 PostBackOptions ops = GetPostBackOptions ();
313                                 if (ops != null && ops.RequiresSpecialPostBack) {
314                                         script += Page.GetPostBackEventReference (ops);
315                                 }
316                                 else if (CausesValidation && Page.Validators.Count > 0) {
317                                         if (UseSubmitBehavior)
318                                                 script += Utils.GetClientValidatedEvent (Page);
319                                         else
320                                                 script += Utils.GetClientValidatedPostBack (this);
321                                 }
322                                 else if (!UseSubmitBehavior) {
323                                         script += Page.GetPostBackClientEvent (this,"");
324                                 }
325 #else
326                                 if (CausesValidation && Page.Validators.Count > 0) {
327                                         script += Utils.GetClientValidatedEvent (Page);
328                                 }
329 #endif
330                                 
331                                 if (script != "") {
332                                         writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick, script);
333                                         writer.AddAttribute ("language", "javascript");
334                                 }
335                         }
336                         
337                         base.AddAttributesToRender (writer);
338                 }
339
340                 protected override void RenderContents(HtmlTextWriter writer)
341                 {
342                         // Preventing base classes to do anything
343                 }
344         }
345 }