2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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         {
48                 private static readonly object ClickEvent   = new object();
49                 private static readonly object CommandEvent = new object();
50
51                 //private EventHandlerList ehList;
52
53                 public Button(): base (HtmlTextWriterTag.Input)
54                 {
55                 }
56
57                 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
58                 [WebSysDescription ("Determines if validation is performed when clicked.")]
59                 public bool CausesValidation
60                 {
61                         get
62                         {
63                                 Object cv = ViewState["CausesValidation"];
64                                 if(cv!=null)
65                                         return (Boolean)cv;
66                                 return true;
67                         }
68                         set
69                         {
70                                 ViewState["CausesValidation"] = value;
71                         }
72                 }
73
74                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
75                 [WebSysDescription ("An argument for the Command of this control.")]
76                 public string CommandArgument
77                 {
78                         get
79                         {
80                                 string ca = (string) ViewState["CommandArgument"];
81                                 if(ca!=null)
82                                         return ca;
83                                 return String.Empty;
84                         }
85                         set
86                         {
87                                 ViewState["CommandArgument"] = value;
88                         }
89                 }
90
91                 [DefaultValue (""), WebCategory ("Behavior")]
92                 [WebSysDescription ("The name of the Command of this control.")]
93                 public string CommandName
94                 {
95                         get
96                         {
97                                 string cn = (string)ViewState["CommandName"];
98                                 if(cn!=null)
99                                         return cn;
100                                 return String.Empty;
101                         }
102                         set
103                         {
104                                 ViewState["CommandName"] = value;
105                         }
106                 }
107
108                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
109                 [WebSysDescription ("The text that should be shown on this Button.")]
110                 public string Text
111                 {
112                         get
113                         {
114                                 string text = (string)ViewState["Text"];
115                                 if(text!=null)
116                                         return text;
117                                 return String.Empty;
118                         }
119                         set
120                         {
121                                 ViewState["Text"] = value;
122                         }
123                 }
124
125                 [WebCategory ("Action")]
126                 [WebSysDescription ("Raised when the Button is clicked.")]
127                 public event EventHandler Click
128                 {
129                         add
130                         {
131                                 Events.AddHandler(ClickEvent, value);
132                         }
133                         remove
134                         {
135                                 Events.RemoveHandler(ClickEvent, value);
136                         }
137                 }
138
139                 [WebCategory ("Action")]
140                 [WebSysDescription ("Raised when a Button Command is executed.")]
141                 public event CommandEventHandler Command
142                 {
143                         add
144                         {
145                                 Events.AddHandler(CommandEvent, value);
146                         }
147                         remove
148                         {
149                                 Events.RemoveHandler(CommandEvent, value);
150                         }
151                 }
152
153                 protected virtual void OnClick(EventArgs e)
154                 {
155                         if(Events != null)
156                         {
157                                 EventHandler eh = (EventHandler)(Events[ClickEvent]);
158                                 if(eh!= null)
159                                         eh(this,e);
160                         }
161                 }
162
163                 protected virtual void OnCommand(CommandEventArgs e)
164                 {
165                         if(Events != null)
166                         {
167                                 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
168                                 if(eh!= null)
169                                         eh(this,e);
170                         }
171                         RaiseBubbleEvent(this, e);
172                 }
173
174                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
175                 {
176                         if (CausesValidation)
177                                 Page.Validate ();
178
179                         OnClick (EventArgs.Empty);
180                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
181                 }
182
183                 protected override void AddAttributesToRender (HtmlTextWriter writer)
184                 {
185                         if (Page != null)
186                                 Page.VerifyRenderingInServerForm (this);
187
188                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
189                         writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
190                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
191                         if (Page != null && CausesValidation && Page.Validators.Count > 0) {
192                                 writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick,
193                                                      Utils.GetClientValidatedEvent (Page));
194                                 writer.AddAttribute ("language", "javascript");
195                         }
196                         base.AddAttributesToRender (writer);
197                 }
198
199                 protected override void RenderContents(HtmlTextWriter writer)
200                 {
201                         // Preventing base classes to do anything
202                 }
203         }
204 }