**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlInputButton.cs
blob3ad014e3612ce8e3b6b97941cd03aa8eaf109135
1 /* System.Web.UI.HtmlControls
2 * Authors
3 * Leen Toelen (toelen@hotmail.com)
4 */
6 using System;
7 using System.ComponentModel;
8 using System.Globalization;
9 using System.Web;
10 using System.Web.UI;
12 namespace System.Web.UI.HtmlControls{
14 [DefaultEvent("ServerClick")]
15 public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler{
17 private static readonly object EventServerClick = new object ();
19 public HtmlInputButton(): base ("button")
23 public HtmlInputButton(string type): base(type){}
25 protected override void OnPreRender (EventArgs e)
27 base.OnPreRender(e);
28 if (Page != null && Events [EventServerClick] != null)
29 Page.RequiresPostBackScript ();
32 protected override void RenderAttributes (HtmlTextWriter writer)
34 if (Page != null && CausesValidation) {
35 string type = Type;
36 if (String.Compare (type, "button", true) == 0 || String.Compare (type, "submit", true) == 0) {
37 string script = Page.GetPostBackClientEvent (this, String.Empty);
38 if (script != null &&
39 ((String.Compare (type, "button", true) == 0 && Events[EventServerClick] != null )||
40 (String.Compare (type, "submit", true) == 0 && Page.Validators.Count > 0))){
41 AttributeCollection coll = Attributes;
42 if (coll ["language"] != null)
43 coll.Remove ("language");
44 writer.WriteAttribute ("language", "javascript");
46 string onclick;
47 if ((onclick = coll ["onclick"]) != null) {
48 script = onclick + " " + script;
49 coll.Remove ("onclick");
52 writer.WriteAttribute ("onclick", script);
57 base.RenderAttributes (writer);
60 protected virtual void OnServerClick(EventArgs e){
61 EventHandler handler = (EventHandler) Events[EventServerClick];
62 if (handler != null){
63 handler (this, e);
67 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
69 if(CausesValidation == true){
70 Page.Validate();
72 OnServerClick(EventArgs.Empty);
75 [WebCategory("Action")]
76 [WebSysDescription("Fires when the control is clicked.")]
77 public event EventHandler ServerClick{
78 add{
79 Events.AddHandler(EventServerClick, value);
81 remove{
82 Events.RemoveHandler(EventServerClick, value);
86 [DefaultValue(true)]
87 [WebCategory("Behavior")]
88 public bool CausesValidation{
89 get{
90 object causesVal = ViewState["CausesValidation"];
91 if (causesVal != null){
92 return (Boolean) causesVal;
94 return true;
96 set{
97 ViewState["CausesValidation"] = (Boolean) value;
101 } // end of System.Web.UI.HtmlControls.HtmlInputButton
102 } // namespace System.Web.UI.HtmlControls