Button.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Web.UI.WebControls.Button.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.Web;
  35. using System.Web.UI;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [DefaultEvent("Click")]
  39. [DefaultProperty("Text")]
  40. [Designer("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  41. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  42. [ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
  43. public class Button : WebControl, IPostBackEventHandler
  44. {
  45. private static readonly object ClickEvent = new object();
  46. private static readonly object CommandEvent = new object();
  47. //private EventHandlerList ehList;
  48. public Button(): base (HtmlTextWriterTag.Input)
  49. {
  50. }
  51. [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
  52. [WebSysDescription ("Determines if validation is performed when clicked.")]
  53. public bool CausesValidation
  54. {
  55. get
  56. {
  57. Object cv = ViewState["CausesValidation"];
  58. if(cv!=null)
  59. return (Boolean)cv;
  60. return true;
  61. }
  62. set
  63. {
  64. ViewState["CausesValidation"] = value;
  65. }
  66. }
  67. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  68. [WebSysDescription ("An argument for the Command of this control.")]
  69. public string CommandArgument
  70. {
  71. get
  72. {
  73. string ca = (string) ViewState["CommandArgument"];
  74. if(ca!=null)
  75. return ca;
  76. return String.Empty;
  77. }
  78. set
  79. {
  80. ViewState["CommandArgument"] = value;
  81. }
  82. }
  83. [DefaultValue (""), WebCategory ("Behavior")]
  84. [WebSysDescription ("The name of the Command of this control.")]
  85. public string CommandName
  86. {
  87. get
  88. {
  89. string cn = (string)ViewState["CommandName"];
  90. if(cn!=null)
  91. return cn;
  92. return String.Empty;
  93. }
  94. set
  95. {
  96. ViewState["CommandName"] = value;
  97. }
  98. }
  99. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  100. [WebSysDescription ("The text that should be shown on this Button.")]
  101. public string Text
  102. {
  103. get
  104. {
  105. string text = (string)ViewState["Text"];
  106. if(text!=null)
  107. return text;
  108. return String.Empty;
  109. }
  110. set
  111. {
  112. ViewState["Text"] = value;
  113. }
  114. }
  115. [WebCategory ("Action")]
  116. [WebSysDescription ("Raised when the Button is clicked.")]
  117. public event EventHandler Click
  118. {
  119. add
  120. {
  121. Events.AddHandler(ClickEvent, value);
  122. }
  123. remove
  124. {
  125. Events.RemoveHandler(ClickEvent, value);
  126. }
  127. }
  128. [WebCategory ("Action")]
  129. [WebSysDescription ("Raised when a Button Command is executed.")]
  130. public event CommandEventHandler Command
  131. {
  132. add
  133. {
  134. Events.AddHandler(CommandEvent, value);
  135. }
  136. remove
  137. {
  138. Events.RemoveHandler(CommandEvent, value);
  139. }
  140. }
  141. protected virtual void OnClick(EventArgs e)
  142. {
  143. if(Events != null)
  144. {
  145. EventHandler eh = (EventHandler)(Events[ClickEvent]);
  146. if(eh!= null)
  147. eh(this,e);
  148. }
  149. }
  150. protected virtual void OnCommand(CommandEventArgs e)
  151. {
  152. if(Events != null)
  153. {
  154. CommandEventHandler eh = (CommandEventHandler)(Events[CommandEvent]);
  155. if(eh!= null)
  156. eh(this,e);
  157. }
  158. RaiseBubbleEvent(this, e);
  159. }
  160. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  161. {
  162. if (CausesValidation)
  163. Page.Validate ();
  164. OnClick (EventArgs.Empty);
  165. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  166. }
  167. protected override void AddAttributesToRender (HtmlTextWriter writer)
  168. {
  169. if (Page != null)
  170. Page.VerifyRenderingInServerForm (this);
  171. writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
  172. writer.AddAttribute (HtmlTextWriterAttribute.Name, base.UniqueID);
  173. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  174. if (Page != null && CausesValidation && Page.Validators.Count > 0) {
  175. writer.AddAttribute (System.Web.UI.HtmlTextWriterAttribute.Onclick,
  176. Utils.GetClientValidatedEvent (Page));
  177. writer.AddAttribute ("language", "javascript");
  178. }
  179. base.AddAttributesToRender (writer);
  180. }
  181. protected override void RenderContents(HtmlTextWriter writer)
  182. {
  183. // Preventing base classes to do anything
  184. }
  185. }
  186. }