* attribute.cs (GetMarshal): Work even if "DefineCustom" is
[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         public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
41
42                 private static readonly object ServerClickEvent = new object();
43
44                 public HtmlButton () : base ("button")
45                 {
46                 }
47
48                 [DefaultValue(true)]
49                 [WebSysDescription("")]
50                 [WebCategory("Behavior")]
51 #if NET_2_0
52                 public virtual
53 #else           
54                 public
55 #endif          
56                 bool CausesValidation {
57                         get {
58                                 return ViewState.GetBool ("CausesValidation", true);
59                         }
60                         set {
61                                 ViewState ["CausesValidation"] = value;
62                         }
63                 }
64
65 #if NET_2_0
66                 [DefaultValue ("")]
67                 public string ValidationGroup 
68                 {
69                         get {
70                                 return ViewState.GetString ("ValidationGroup", "");
71                         }
72                         set {
73                                 ViewState ["ValidationGroup"] = value;
74                         }
75                 }
76 #endif          
77
78 #if NET_2_0
79                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
80                 {
81                         RaisePostBackEvent (eventArgument);
82                 }
83
84                 protected virtual void RaisePostBackEvent (string eventArgument)
85 #else
86                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
87 #endif
88                 {
89                         if (CausesValidation)
90 #if NET_2_0
91                                 Page.Validate (ValidationGroup);
92 #else
93                                 Page.Validate ();
94 #endif
95                         OnServerClick (EventArgs.Empty);
96                 }
97
98 #if NET_2_0
99                 protected internal
100 #else           
101                 protected
102 #endif
103                 override void OnPreRender (EventArgs e)
104                 {
105                         base.OnPreRender (e);
106                 }
107
108                 protected virtual void OnServerClick (EventArgs e)
109                 {
110                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
111                         if (server_click != null)
112                                 server_click (this, e);
113                 }
114
115                 protected override void RenderAttributes (HtmlTextWriter writer)
116                 {
117                         ClientScriptManager csm = new ClientScriptManager (Page);
118                         bool postback = false;
119
120                         if (Page != null && Events [ServerClickEvent] != null)
121                                 postback = true;
122
123                         if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
124                                 if (postback)
125                                         writer.WriteAttribute ("onclick",
126                                                                String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) {0}}}",
127                                                                               csm.GetPostBackEventReference (this, String.Empty)));
128                                 else
129                                         writer.WriteAttribute ("onclick",
130                                                                "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
131
132                                 writer.WriteAttribute ("language", "javascript");
133                         }
134                         else if (postback) {
135                                 writer.AddAttribute ("onclick",
136                                                      Page.ClientScript.GetPostBackClientHyperlink (this, ""));
137
138                                 writer.WriteAttribute ("language", "javascript");
139                         }
140
141                         base.RenderAttributes (writer);
142                 }
143
144                 [WebSysDescription("")]
145                 [WebCategory("Action")]
146                 public event EventHandler ServerClick {
147                         add { Events.AddHandler (ServerClickEvent, value); }
148                         remove { Events.RemoveHandler (ServerClickEvent, value); }
149                 }
150         }
151
152 }
153