Merge branch 'master' into ms-decimal
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlButton.cs
index 14e3dd9d4e4ac153bdbb71097ca960fe2eb40df6..e61a051b446cd9ce13570e3ea9643fa23655e30c 100644 (file)
@@ -24,7 +24,7 @@
 // Authors:
 //     Jackson Harper (jackson@ximian.com)
 //
-// (C) 2005 Novell, Inc.
+// (C) 2005-2010 Novell, Inc.
 
 
 using System.ComponentModel;
@@ -37,9 +37,10 @@ namespace System.Web.UI.HtmlControls {
        [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        // attributes
        [DefaultEvent("ServerClick")]
+       [SupportsEventValidation]
        public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
 
-               private static readonly object ServerClickEvent = new object();
+               static readonly object ServerClickEvent = new object();
 
                public HtmlButton () : base ("button")
                {
@@ -48,12 +49,7 @@ namespace System.Web.UI.HtmlControls {
                [DefaultValue(true)]
                [WebSysDescription("")]
                [WebCategory("Behavior")]
-#if NET_2_0
-               public virtual
-#else          
-               public
-#endif         
-               bool CausesValidation {
+               public virtual bool CausesValidation {
                        get {
                                return ViewState.GetBool ("CausesValidation", true);
                        }
@@ -62,9 +58,8 @@ namespace System.Web.UI.HtmlControls {
                        }
                }
 
-#if NET_2_0
                [DefaultValue ("")]
-               public string ValidationGroup 
+               public virtual string ValidationGroup 
                {
                        get {
                                return ViewState.GetString ("ValidationGroup", "");
@@ -73,34 +68,20 @@ namespace System.Web.UI.HtmlControls {
                                ViewState ["ValidationGroup"] = value;
                        }
                }
-#endif         
-
-#if NET_2_0
                void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
                {
                        RaisePostBackEvent (eventArgument);
                }
 
                protected virtual void RaisePostBackEvent (string eventArgument)
-#else
-               void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
-#endif
                {
+                       ValidateEvent (UniqueID, eventArgument);
                        if (CausesValidation)
-#if NET_2_0
                                Page.Validate (ValidationGroup);
-#else
-                               Page.Validate ();
-#endif
                        OnServerClick (EventArgs.Empty);
                }
-
-#if NET_2_0
-               protected internal
-#else          
-               protected
-#endif
-               override void OnPreRender (EventArgs e)
+               
+               protected internal override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
                }
@@ -114,33 +95,32 @@ namespace System.Web.UI.HtmlControls {
 
                protected override void RenderAttributes (HtmlTextWriter writer)
                {
-                       ClientScriptManager csm = new ClientScriptManager (Page);
-                       bool postback = false;
-
-                       if (Page != null && Events [ServerClickEvent] != null)
-                               postback = true;
-
-                       if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
-                               if (postback)
-                                       writer.WriteAttribute ("onclick",
-                                                              String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) {0}}}",
-                                                                             csm.GetPostBackEventReference (this, String.Empty)));
-                               else
-                                       writer.WriteAttribute ("onclick",
-                                                              "if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();");
-
-                               writer.WriteAttribute ("language", "javascript");
-                       }
-                       else if (postback) {
-                               writer.WriteAttribute ("onclick",
-                                                      Page.ClientScript.GetPostBackClientHyperlink (this, ""));
-
+                       Page page = Page;
+                       if (page != null && Events [ServerClickEvent] != null) {
+                               PostBackOptions options = GetPostBackOptions ();
+                               Attributes ["onclick"] += page.ClientScript.GetPostBackEventReference (options, true);
                                writer.WriteAttribute ("language", "javascript");
                        }
 
                        base.RenderAttributes (writer);
                }
 
+               PostBackOptions GetPostBackOptions ()
+               {
+                       Page page = Page;
+                       PostBackOptions options = new PostBackOptions (this);
+                       options.ValidationGroup = null;
+                       options.ActionUrl = null;
+                       options.Argument = String.Empty;
+                       options.RequiresJavaScriptProtocol = false;
+                       options.ClientSubmit = true;
+                       options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
+                       if (options.PerformValidation)
+                               options.ValidationGroup = ValidationGroup;
+
+                       return options;
+               }
+
                [WebSysDescription("")]
                [WebCategory("Action")]
                public event EventHandler ServerClick {
@@ -148,6 +128,5 @@ namespace System.Web.UI.HtmlControls {
                        remove { Events.RemoveHandler (ServerClickEvent, value); }
                }
        }
-
 }