2010-03-12 Jb Evain <jbevain@novell.com>
[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                 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 NET_2_0
93                         ValidateEvent (UniqueID, eventArgument);
94 #endif
95                         if (CausesValidation)
96 #if NET_2_0
97                                 Page.Validate (ValidationGroup);
98 #else
99                                 Page.Validate ();
100 #endif
101                         OnServerClick (EventArgs.Empty);
102                 }
103                 
104 #if NET_2_0
105                 protected internal
106 #else           
107                 protected
108 #endif
109                 override void OnPreRender (EventArgs e)
110                 {
111                         base.OnPreRender (e);
112                 }
113
114                 protected virtual void OnServerClick (EventArgs e)
115                 {
116                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
117                         if (server_click != null)
118                                 server_click (this, e);
119                 }
120
121                 protected override void RenderAttributes (HtmlTextWriter writer)
122                 {
123 #if NET_2_0
124                         if (Page != null && Events [ServerClickEvent] != null) {
125                                 PostBackOptions options = GetPostBackOptions ();
126                                 Attributes ["onclick"] += Page.ClientScript.GetPostBackEventReference (options, true);
127                                 writer.WriteAttribute ("language", "javascript");
128                         }
129 #else           
130                         ClientScriptManager csm = new ClientScriptManager (Page);
131                         bool postback = false;
132
133                         if (Page != null && Events [ServerClickEvent] != null)
134                                 postback = true;
135
136                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
137                                 if (postback)
138                                         writer.WriteAttribute ("onclick",
139                                                                String.Concat ("javascript:{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) ",
140                                                                               csm.GetPostBackEventReference (this, String.Empty), "}"));
141                                 else
142                                         writer.WriteAttribute ("onclick",
143                                                                "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
144
145                                 writer.WriteAttribute ("language", "javascript");
146                         }
147                         else if (postback) {
148                                 writer.WriteAttribute ("onclick",
149                                                        Page.ClientScript.GetPostBackClientHyperlink (this, ""));
150
151                                 writer.WriteAttribute ("language", "javascript");
152                         }
153 #endif
154
155                         base.RenderAttributes (writer);
156                 }
157
158 #if NET_2_0
159                 PostBackOptions GetPostBackOptions () {
160                         PostBackOptions options = new PostBackOptions (this);
161                         options.ValidationGroup = null;
162                         options.ActionUrl = null;
163                         options.Argument = String.Empty;
164                         options.RequiresJavaScriptProtocol = false;
165                         options.ClientSubmit = true;
166                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
167                         if (options.PerformValidation)
168                                 options.ValidationGroup = ValidationGroup;
169
170                         return options;
171                 }
172 #endif
173
174                 [WebSysDescription("")]
175                 [WebCategory("Action")]
176                 public event EventHandler ServerClick {
177                         add { Events.AddHandler (ServerClickEvent, value); }
178                         remove { Events.RemoveHandler (ServerClickEvent, value); }
179                 }
180         }
181
182 }
183