New tests.
[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-2010 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         [SupportsEventValidation]
41         public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
42
43                 static readonly object ServerClickEvent = new object();
44
45                 public HtmlButton () : base ("button")
46                 {
47                 }
48
49                 [DefaultValue(true)]
50                 [WebSysDescription("")]
51                 [WebCategory("Behavior")]
52                 public virtual bool CausesValidation {
53                         get {
54                                 return ViewState.GetBool ("CausesValidation", true);
55                         }
56                         set {
57                                 ViewState ["CausesValidation"] = value;
58                         }
59                 }
60
61                 [DefaultValue ("")]
62                 public virtual string ValidationGroup 
63                 {
64                         get {
65                                 return ViewState.GetString ("ValidationGroup", "");
66                         }
67                         set {
68                                 ViewState ["ValidationGroup"] = value;
69                         }
70                 }
71                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
72                 {
73                         RaisePostBackEvent (eventArgument);
74                 }
75
76                 protected virtual void RaisePostBackEvent (string eventArgument)
77                 {
78                         ValidateEvent (UniqueID, eventArgument);
79                         if (CausesValidation)
80                                 Page.Validate (ValidationGroup);
81                         OnServerClick (EventArgs.Empty);
82                 }
83                 
84                 protected internal override void OnPreRender (EventArgs e)
85                 {
86                         base.OnPreRender (e);
87                 }
88
89                 protected virtual void OnServerClick (EventArgs e)
90                 {
91                         EventHandler server_click = (EventHandler) Events [ServerClickEvent];
92                         if (server_click != null)
93                                 server_click (this, e);
94                 }
95
96                 protected override void RenderAttributes (HtmlTextWriter writer)
97                 {
98                         Page page = Page;
99                         if (page != null && Events [ServerClickEvent] != null) {
100                                 PostBackOptions options = GetPostBackOptions ();
101                                 Attributes ["onclick"] += page.ClientScript.GetPostBackEventReference (options, true);
102                                 writer.WriteAttribute ("language", "javascript");
103                         }
104
105                         base.RenderAttributes (writer);
106                 }
107
108                 PostBackOptions GetPostBackOptions ()
109                 {
110                         Page page = Page;
111                         PostBackOptions options = new PostBackOptions (this);
112                         options.ValidationGroup = null;
113                         options.ActionUrl = null;
114                         options.Argument = String.Empty;
115                         options.RequiresJavaScriptProtocol = false;
116                         options.ClientSubmit = true;
117                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
118                         if (options.PerformValidation)
119                                 options.ValidationGroup = ValidationGroup;
120
121                         return options;
122                 }
123
124                 [WebSysDescription("")]
125                 [WebCategory("Action")]
126                 public event EventHandler ServerClick {
127                         add { Events.AddHandler (ServerClickEvent, value); }
128                         remove { Events.RemoveHandler (ServerClickEvent, value); }
129                 }
130         }
131 }
132