2002-08-19 Gaurav Vaish <gvaish_mono@lycos.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Button.cs
1 /**
2  * Namespace: System.Web.UI.WebControls
3  * Class:     Button
4  *
5  * Author:  Gaurav Vaish
6  * Maintainer: gvaish@iitk.ac.in
7  * Contact: <my_scripts2001@yahoo.com>, <gvaish@iitk.ac.in>
8  * Implementation: yes
9  * Status:  100%
10  *
11  * (C) Gaurav Vaish (2001)
12  */
13
14 using System;
15 using System.ComponentModel;
16 using System.Web;
17 using System.Web.UI;
18
19 namespace System.Web.UI.WebControls
20 {
21         [DefaultEvent("Click")]
22         [DefaultProperty("Text")]
23         //TODO: [Designer("??")]
24         //TODO: [DataBindingHandler("??UI.Design.TextDataBindingHandler??")]
25         [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
26         public class Button : WebControl, IPostBackEventHandler
27         {
28                 private static readonly object ClickEvent   = new object();
29                 private static readonly object CommandEvent = new object();
30
31                 //private EventHandlerList ehList;
32
33                 public Button(): base (HtmlTextWriterTag.Input)
34                 {
35                 }
36
37                 public bool CausesValidation
38                 {
39                         get
40                         {
41                                 Object cv = ViewState["CausesValidation"];
42                                 if(cv!=null)
43                                         return (Boolean)cv;
44                                 return true;
45                         }
46                         set
47                         {
48                                 ViewState["CausesValidation"] = value;
49                         }
50                 }
51
52                 public string CommandArgument
53                 {
54                         get
55                         {
56                                 string ca = (string) ViewState["CommandArgument"];
57                                 if(ca!=null)
58                                         return ca;
59                                 return String.Empty;
60                         }
61                         set
62                         {
63                                 ViewState["CommandArgument"] = value;
64                         }
65                 }
66
67                 public string CommandName
68                 {
69                         get
70                         {
71                                 string cn = (string)ViewState["CommandName"];
72                                 if(cn!=null)
73                                         return cn;
74                                 return String.Empty;
75                         }
76                         set
77                         {
78                                 ViewState["CommandName"] = value;
79                         }
80                 }
81
82                 public string Text
83                 {
84                         get
85                         {
86                                 string text = (string)ViewState["Text"];
87                                 if(text!=null)
88                                         return text;
89                                 return String.Empty;
90                         }
91                         set
92                         {
93                                 ViewState["Text"] = value;
94                         }
95                 }
96
97                 public event EventHandler Click
98                 {
99                         add
100                         {
101                                 Events.AddHandler(ClickEvent, value);
102                         }
103                         remove
104                         {
105                                 Events.RemoveHandler(ClickEvent, value);
106                         }
107                 }
108
109                 public event CommandEventHandler Command
110                 {
111                         add
112                         {
113                                 Events.AddHandler(CommandEvent, value);
114                         }
115                         remove
116                         {
117                                 Events.RemoveHandler(CommandEvent, value);
118                         }
119                 }
120
121                 protected virtual void OnClick(EventArgs e)
122                 {
123                         if(Events != null)
124                         {
125                                 EventHandler eh = (EventHandler)(Events[ClickEvent]);
126                                 if(eh!= null)
127                                         eh(this,e);
128                         }
129                 }
130
131                 protected virtual void OnCommand(CommandEventArgs e)
132                 {
133                         if(Events != null)
134                         {
135                                 CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
136                                 if(eh!= null)
137                                         eh(this,e);
138                         }
139                         RaiseBubbleEvent(this, e);
140                 }
141
142                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
143                 {
144                         if (CausesValidation)
145                                 Page.Validate ();
146
147                         OnClick (EventArgs.Empty);
148                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
149                 }
150
151                 protected override void AddAttributesToRender (HtmlTextWriter writer)
152                 {
153                         if (Page != null)
154                                 Page.VerifyRenderingInServerForm (this);
155
156                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
157                         writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
158                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
159                         if (Page != null && CausesValidation && Page.Validators.Count > 0) {
160                                 writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick,
161                                                      Utils.GetClientValidatedEvent (Page));
162                                 writer.AddAttribute ("language", "javascript");
163                         }
164                         base.AddAttributesToRender (writer);
165                 }
166
167                 protected override void RenderContents(HtmlTextWriter writer)
168                 {
169                         // Preventing base classes to do anything
170                 }
171         }
172 }