2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.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 using System;
13 using System.ComponentModel;
14 using System.ComponentModel.Design;
15 using System.Web;
16 using System.Web.UI;
17
18 namespace System.Web.UI.WebControls
19 {
20         [DefaultEvent("Click")]
21         [DefaultProperty("Text")]
22         [Designer("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]\r
23         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
24         [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
25         public class Button : WebControl, IPostBackEventHandler
26         {
27                 private static readonly object ClickEvent   = new object();
28                 private static readonly object CommandEvent = new object();
29
30                 //private EventHandlerList ehList;
31
32                 public Button(): base (HtmlTextWriterTag.Input)
33                 {
34                 }
35
36                 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
37                 [WebSysDescription ("Determines if validation is performed when clicked.")]
38                 public bool CausesValidation
39                 {
40                         get
41                         {
42                                 Object cv = ViewState["CausesValidation"];
43                                 if(cv!=null)
44                                         return (Boolean)cv;
45                                 return true;
46                         }
47                         set
48                         {
49                                 ViewState["CausesValidation"] = value;
50                         }
51                 }
52
53                 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
54                 [WebSysDescription ("An argument for the Command of this control.")]
55                 public string CommandArgument
56                 {
57                         get
58                         {
59                                 string ca = (string) ViewState["CommandArgument"];
60                                 if(ca!=null)
61                                         return ca;
62                                 return String.Empty;
63                         }
64                         set
65                         {
66                                 ViewState["CommandArgument"] = value;
67                         }
68                 }
69
70                 [DefaultValue (""), WebCategory ("Behavior")]
71                 [WebSysDescription ("The name of the Command of this control.")]
72                 public string CommandName
73                 {
74                         get
75                         {
76                                 string cn = (string)ViewState["CommandName"];
77                                 if(cn!=null)
78                                         return cn;
79                                 return String.Empty;
80                         }
81                         set
82                         {
83                                 ViewState["CommandName"] = value;
84                         }
85                 }
86
87                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
88                 [WebSysDescription ("The text that should be shown on this Button.")]
89                 public string Text
90                 {
91                         get
92                         {
93                                 string text = (string)ViewState["Text"];
94                                 if(text!=null)
95                                         return text;
96                                 return String.Empty;
97                         }
98                         set
99                         {
100                                 ViewState["Text"] = value;
101                         }
102                 }
103
104                 [WebCategory ("Action")]
105                 [WebSysDescription ("Raised when the Button is clicked.")]
106                 public event EventHandler Click
107                 {
108                         add
109                         {
110                                 Events.AddHandler(ClickEvent, value);
111                         }
112                         remove
113                         {
114                                 Events.RemoveHandler(ClickEvent, value);
115                         }
116                 }
117
118                 [WebCategory ("Action")]
119                 [WebSysDescription ("Raised when a Button Command is executed.")]
120                 public event CommandEventHandler Command
121                 {
122                         add
123                         {
124                                 Events.AddHandler(CommandEvent, value);
125                         }
126                         remove
127                         {
128                                 Events.RemoveHandler(CommandEvent, value);
129                         }
130                 }
131
132                 protected virtual void OnClick(EventArgs e)
133                 {
134                         if(Events != null)
135                         {
136                                 EventHandler eh = (EventHandler)(Events[ClickEvent]);
137                                 if(eh!= null)
138                                         eh(this,e);
139                         }
140                 }
141
142                 protected virtual void OnCommand(CommandEventArgs e)
143                 {
144                         if(Events != null)
145                         {
146                                 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
147                                 if(eh!= null)
148                                         eh(this,e);
149                         }
150                         RaiseBubbleEvent(this, e);
151                 }
152
153                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
154                 {
155                         if (CausesValidation)
156                                 Page.Validate ();
157
158                         OnClick (EventArgs.Empty);
159                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
160                 }
161
162                 protected override void AddAttributesToRender (HtmlTextWriter writer)
163                 {
164                         if (Page != null)
165                                 Page.VerifyRenderingInServerForm (this);
166
167                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
168                         writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
169                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
170                         if (Page != null && CausesValidation && Page.Validators.Count > 0) {
171                                 writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick,
172                                                      Utils.GetClientValidatedEvent (Page));
173                                 writer.AddAttribute ("language", "javascript");
174                         }
175                         base.AddAttributesToRender (writer);
176                 }
177
178                 protected override void RenderContents(HtmlTextWriter writer)
179                 {
180                         // Preventing base classes to do anything
181                 }
182         }
183 }