New test.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputButton.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlInputButton.cs
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7 // (C) 2005 Novell, Inc.
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.Globalization;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.HtmlControls {
34
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [DefaultEventAttribute ("ServerClick")]
40 #if NET_2_0
41         [SupportsEventValidation]
42 #endif
43         public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
44
45                 private static readonly object ServerClickEvent = new object();
46
47                 public HtmlInputButton () : this ("button")
48                 {
49                 }
50
51                 public HtmlInputButton (string type) :  base (type)
52                 {
53                 }
54
55                 [DefaultValue(true)]
56                 [WebSysDescription("")]
57                 [WebCategory("Behavior")]
58 #if NET_2_0
59                 public virtual
60 #else           
61                 public
62 #endif          
63                 bool CausesValidation {
64                         get {
65                                 string flag = Attributes["CausesValidation"];
66
67                                 if (flag == null)
68                                         return true;
69
70                                 return Boolean.Parse (flag);
71                         }
72                         set {
73                                 Attributes ["CausesValidation"] = value.ToString();
74                         }
75                 }
76
77 #if NET_2_0
78                 [DefaultValue ("")]
79                 public virtual string ValidationGroup
80                 {
81                         get {
82                                 string group = Attributes["ValidationGroup"];
83
84                                 if (group == null)
85                                         return "";
86
87                                 return group;
88                         }
89                         set {
90                                 if (value == null)
91                                         Attributes.Remove ("ValidationGroup");
92                                 else
93                                         Attributes["ValidationGroup"] = value;
94                         }
95                 }
96 #endif
97
98                 void RaisePostBackEventInternal (string eventArgument)
99                 {
100                         if (CausesValidation)
101 #if NET_2_0
102                                 Page.Validate (ValidationGroup);
103 #else
104                                 Page.Validate ();
105 #endif
106                         OnServerClick (EventArgs.Empty);
107                 }
108
109 #if NET_2_0
110                 protected virtual void RaisePostBackEvent (string eventArgument)
111                 {
112                         RaisePostBackEventInternal (eventArgument);
113                 }
114 #endif
115                 
116                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
117                 {
118 #if NET_2_0
119                         RaisePostBackEvent (eventArgument);
120 #else
121                         RaisePostBackEventInternal (eventArgument);
122 #endif
123                 }
124
125 #if NET_2_0
126                 protected internal
127 #else           
128                 protected
129 #endif          
130                 override void OnPreRender (EventArgs e)
131                 {
132                         base.OnPreRender (e);
133                         if (Events [ServerClickEvent] != null)
134                                 Page.RequiresPostBackScript ();
135                 }
136
137                 protected virtual void OnServerClick (EventArgs e)
138                 {
139                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
140                         if (server_click != null)
141                                 server_click (this, e);
142                 }
143
144 #if !NET_2_0
145                 bool RenderOnClick ()
146                 {
147                         if (Page == null || !CausesValidation)
148                                 return false;
149
150                         CultureInfo inv = CultureInfo.InvariantCulture;
151                         string input_type = Type;
152                         if (0 == String.Compare (input_type, "submit", true, inv) &&
153                                 Page.Validators.Count > 0)
154                                 return true;
155
156                         if (0 == String.Compare (input_type, "button", true, inv) &&
157                                 Events [ServerClickEvent] != null)
158                                 return true;
159
160                         return false;
161                 }
162 #endif
163
164                 protected override void RenderAttributes (HtmlTextWriter writer)
165                 {
166 #if NET_2_0
167                         CultureInfo inv = CultureInfo.InvariantCulture;
168                         string input_type = Type;
169                         if (0 != String.Compare (input_type, "reset", true, inv) &&
170                                 ((0 == String.Compare (input_type, "submit", true, inv)) ||
171                                 (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
172
173                                 string onclick = String.Empty;
174                                 if (Attributes ["onclick"] != null) {
175                                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
176                                         Attributes.Remove ("onclick");
177                                 }
178                                 if (Page != null) {
179                                         PostBackOptions options = GetPostBackOptions ();
180                                         Page.ClientScript.RegisterForEventValidation (options);
181                                         onclick += Page.ClientScript.GetPostBackEventReference (options);
182                                 }
183
184                                 if (onclick.Length > 0) {
185                                         writer.WriteAttribute ("onclick", onclick);
186                                         writer.WriteAttribute ("language", "javascript");
187                                 }
188                         }
189 #else
190                         if (RenderOnClick ()) {
191                                 string oc = null;
192                                 ClientScriptManager csm = new ClientScriptManager (Page);
193                                 if (Page.AreValidatorsUplevel ()) {
194                                         oc = csm.GetClientValidationEvent ();
195                                 } else if (Events [ServerClickEvent] != null) {
196                                         oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
197                                 }
198                                 
199                                 if (oc != null) {
200                                         writer.WriteAttribute ("language", "javascript");
201                                         writer.WriteAttribute ("onclick", oc);
202                                 }
203                         }
204 #endif
205
206                         Attributes.Remove ("CausesValidation");
207 #if NET_2_0
208                         // LAMESPEC: MS doesn't actually remove this
209                         //attribute.  it shows up in the rendered
210                         //output.
211
212                         // Attributes.Remove("ValidationGroup");
213 #endif
214                         base.RenderAttributes (writer);
215                 }
216
217 #if NET_2_0
218                 PostBackOptions GetPostBackOptions () {
219                         PostBackOptions options = new PostBackOptions (this);
220                         options.ValidationGroup = null;
221                         options.ActionUrl = null;
222                         options.Argument = "";
223                         options.RequiresJavaScriptProtocol = false;
224                         options.ClientSubmit = (0 != String.Compare (Type, "submit", true, CultureInfo.InvariantCulture));
225                         options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
226                         if (options.PerformValidation)
227                                 options.ValidationGroup = ValidationGroup;
228
229                         return options;
230                 }
231 #endif
232
233                 [WebSysDescription("")]
234                 [WebCategory("Action")]
235                 public event EventHandler ServerClick {
236                         add { Events.AddHandler (ServerClickEvent, value); }
237                         remove { Events.RemoveHandler (ServerClickEvent, value); }
238                 }
239         }
240         
241 }
242