svn path=/branches/mono-1-1-9/mcs/; revision=51212
[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
32 namespace System.Web.UI.WebControls {
33
34         [DefaultEvent ("Click")]
35         [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
36         [DefaultProperty ("Text")]
37         [Designer ("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
38
39 #if NET_2_0
40         [ToolboxDataAttribute ("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
41 #else
42         [ToolboxDataAttribute ("<{0}:Button runat=server Text=\"Button\"></{0}:Button>")]
43 #endif
44
45         public class Button : WebControl, IPostBackEventHandler
46 #if NET_2_0
47         , IButtonControl
48 #endif
49         {
50
51                 private static readonly object ClickEvent = new object ();
52                 private static readonly object CommandEvent = new object ();
53
54                 public Button () : base (HtmlTextWriterTag.Input)
55                 {
56                 }
57
58 #if ONLY_1_1
59                 [Bindable (false)]
60 #endif          
61                 [WebSysDescription ("")]
62                 [WebCategory ("Behavior")]
63                 [DefaultValue (true)]
64 #if NET_2_0
65                 [Themeable (false)]
66                 public virtual
67 #else           
68                 public
69 #endif          
70                 bool CausesValidation {
71                         get {
72                                 return ViewState.GetBool ("CausesValidation", true);
73                         }
74
75                         set {
76                                 ViewState ["CausesValidation"] = value;
77                         }
78                 }
79
80                 [DefaultValue ("")]
81                 [Bindable (true)]
82                 [WebSysDescription ("")]
83                 [WebCategory ("Behavior")]
84 #if NET_2_0
85                 [Themeable (false)]
86                 public virtual
87 #else           
88                 public
89 #endif          
90                 string CommandArgument {
91                         get {
92                                 return ViewState.GetString ("CommandArgument", "");
93                         }
94                         set {
95                                 ViewState ["CommandArgument"] = value;
96                         }
97                 }
98
99                 [DefaultValue ("")]
100                 [WebSysDescription ("")]
101                 [WebCategory ("Behavior")]
102 #if NET_2_0
103                 [Themeable (false)]
104                 public virtual
105 #else
106                 public
107 #endif          
108                 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                 public virtual
142 #else           
143                 public
144 #endif          
145                 string Text {
146                         get {
147                                 return ViewState.GetString ("Text", "");
148                         }
149                         set {
150                                 ViewState ["Text"] = value;
151                         }
152                 }
153
154 #if NET_2_0
155                 [DefaultValue (true)]
156                 [Themeable (false)]
157                 [MonoTODO]
158                 [WebSysDescription ("")]
159                 [WebCategoryAttribute ("Behavior")]
160                 public virtual bool UseSubmitBehavior 
161                 {
162                         get {
163                                 throw new NotImplementedException ();
164                         }
165                         set {
166                                 throw new NotImplementedException ();
167                         }
168                 }
169 #endif          
170
171                 protected override void AddAttributesToRender (HtmlTextWriter writer)
172                 {
173                         if (Page != null)
174                                 Page.VerifyRenderingInServerForm (this);
175
176                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
177                         writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
178                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
179
180                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
181                                 ClientScriptManager csm = new ClientScriptManager (Page);
182                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, csm.GetClientValidationEvent ());
183                                 writer.AddAttribute ("language", "javascript");
184                         }
185
186                         base.AddAttributesToRender (writer);
187                 }
188
189 #if NET_2_0
190                 [MonoTODO]
191                 protected virtual PostBackOptions GetPostBackOptions ()
192                 {
193                         throw new NotImplementedException ();
194                 }
195 #endif          
196
197                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
198                 {
199                         RaisePostBackEvent (eventArgument);
200                 }
201
202                 protected virtual void OnClick (EventArgs e)
203                 {
204                         if (Events != null) {
205                                 EventHandler eh = (EventHandler) (Events [ClickEvent]);
206                                 if (eh != null)
207                                         eh (this, e);
208                         }
209                 }
210
211                 protected virtual void OnCommand (CommandEventArgs e)
212                 {
213                         if (Events != null) {
214                                 CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
215                                 if (eh != null)
216                                         eh (this, e);
217                         }
218
219                         RaiseBubbleEvent (this, e);
220                 }
221
222 #if NET_2_0
223                 protected virtual
224 #endif
225                 void RaisePostBackEvent (string eventArgument)
226                 {
227                         if (CausesValidation)
228 #if NET_2_0
229                                 Page.Validate (ValidationGroup);
230 #else
231                                 Page.Validate ();
232 #endif
233
234                         OnClick (EventArgs.Empty);
235                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
236                 }
237
238 #if NET_2_0
239                 protected internal
240 #else           
241                 protected
242 #endif          
243                 override void RenderContents (HtmlTextWriter writer)
244                 {
245                 }
246
247                 [WebSysDescription ("")]
248                 [WebCategory ("Action")]
249                 public event EventHandler Click
250                 {
251                         add {
252                                 Events.AddHandler (ClickEvent, value);
253                         }
254                         remove {
255                                 Events.RemoveHandler (ClickEvent, value);
256                         }
257                 }
258
259                 [WebSysDescription ("")]
260                 [WebCategory ("Action")]
261                 public event CommandEventHandler Command
262                 {
263                         add {
264                                 Events.AddHandler (CommandEvent, value);
265                         }
266                         remove {
267                                 Events.RemoveHandler (CommandEvent, value);
268                         }
269                 }
270
271 #if NET_2_0
272                 [DefaultValue ("")]
273                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
274                 [Themeable (false)]
275                 [UrlProperty("*.aspx")]
276                 [MonoTODO]
277                 public string PostBackUrl {
278                         get {
279                                 throw new NotImplementedException ();
280                         }
281                         set {
282                                 throw new NotImplementedException ();
283                         }
284                 }
285
286                 [DefaultValue ("")]
287                 [Themeable (false)]
288                 [WebSysDescription ("")]
289                 [WebCategoryAttribute ("Behavior")]
290                 public string ValidationGroup {
291                         get {
292                                 return ViewState.GetString ("ValidationGroup", "");
293                         }
294                         set {
295                                 ViewState ["ValidationGroup"] = value;
296                         }
297                 }       
298 #endif
299
300         }
301 }
302