Moved ProviderCollectionTest.cs from System assembly to System.Configuration.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlButton.cs
1 //
2 // Permission is hereby granted, free of charge, to any person obtaining
3 // a copy of this software and associated documentation files (the
4 // "Software"), to deal in the Software without restriction, including
5 // without limitation the rights to use, copy, modify, merge, publish,
6 // distribute, sublicense, and/or sell copies of the Software, and to
7 // permit persons to whom the Software is furnished to do so, subject to
8 // the following conditions:
9 // 
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
12 // 
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
17 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //
22 // System.Web.UI.HtmlControls.HtmlButton
23 //
24 // Authors:
25 //      Jackson Harper (jackson@ximian.com)
26 //
27 // (C) 2005 Novell, Inc.
28
29
30 using System.ComponentModel;
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         [DefaultEvent("ServerClick")]
40 #if NET_2_0
41         [SupportsEventValidation]
42 #endif
43         public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
44
45                 private static readonly object ServerClickEvent = new object();
46
47                 public HtmlButton () : base ("button")
48                 {
49                 }
50
51                 [DefaultValue(true)]
52                 [WebSysDescription("")]
53                 [WebCategory("Behavior")]
54 #if NET_2_0
55                 public virtual
56 #else           
57                 public
58 #endif          
59                 bool CausesValidation {
60                         get {
61                                 return ViewState.GetBool ("CausesValidation", true);
62                         }
63                         set {
64                                 ViewState ["CausesValidation"] = value;
65                         }
66                 }
67
68 #if NET_2_0
69                 [DefaultValue ("")]
70                 public virtual string ValidationGroup 
71                 {
72                         get {
73                                 return ViewState.GetString ("ValidationGroup", "");
74                         }
75                         set {
76                                 ViewState ["ValidationGroup"] = value;
77                         }
78                 }
79 #endif          
80
81 #if NET_2_0
82                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
83                 {
84                         RaisePostBackEvent (eventArgument);
85                 }
86
87                 protected virtual void RaisePostBackEvent (string eventArgument)
88 #else
89                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
90 #endif
91                 {
92                         if (CausesValidation)
93 #if NET_2_0
94                                 Page.Validate (ValidationGroup);
95 #else
96                                 Page.Validate ();
97 #endif
98                         OnServerClick (EventArgs.Empty);
99                 }
100                 
101 #if NET_2_0
102                 protected internal
103 #else           
104                 protected
105 #endif
106                 override void OnPreRender (EventArgs e)
107                 {
108                         base.OnPreRender (e);
109                 }
110
111                 protected virtual void OnServerClick (EventArgs e)
112                 {
113                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
114                         if (server_click != null)
115                                 server_click (this, e);
116                 }
117
118                 protected override void RenderAttributes (HtmlTextWriter writer)
119                 {
120 #if NET_2_0
121                         if (Page != null && Events [ServerClickEvent] != null) {
122                                 PostBackOptions options = GetPostBackOptions ();
123                                 Page.ClientScript.RegisterForEventValidation (options);
124                                 Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options);
125                                 writer.WriteAttribute ("language", "javascript");
126                         }
127 #else           
128                         ClientScriptManager csm = new ClientScriptManager (Page);
129                         bool postback = false;
130
131                         if (Page != null && Events [ServerClickEvent] != null)
132                                 postback = true;
133
134                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
135                                 if (postback)
136                                         writer.WriteAttribute ("onclick",
137                                                                String.Concat ("javascript:{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) ",
138                                                                               csm.GetPostBackEventReference (this, String.Empty), "}"));
139                                 else
140                                         writer.WriteAttribute ("onclick",
141                                                                "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
142
143                                 writer.WriteAttribute ("language", "javascript");
144                         }
145                         else if (postback) {
146                                 writer.WriteAttribute ("onclick",
147                                                        Page.ClientScript.GetPostBackClientHyperlink (this, ""));
148
149                                 writer.WriteAttribute ("language", "javascript");
150                         }
151 #endif
152
153                         base.RenderAttributes (writer);
154                 }
155
156 #if NET_2_0
157                 PostBackOptions GetPostBackOptions () {
158                         PostBackOptions options = new PostBackOptions (this);
159                         options.ValidationGroup = null;
160                         options.ActionUrl = null;
161                         options.Argument = "";
162                         options.RequiresJavaScriptProtocol = false;
163                         options.ClientSubmit = true;
164                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
165                         if (options.PerformValidation)
166                                 options.ValidationGroup = ValidationGroup;
167
168                         return options;
169                 }
170 #endif
171
172                 [WebSysDescription("")]
173                 [WebCategory("Action")]
174                 public event EventHandler ServerClick {
175                         add { Events.AddHandler (ServerClickEvent, value); }
176                         remove { Events.RemoveHandler (ServerClickEvent, value); }
177                 }
178         }
179
180 }
181