Button.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // System.Web.UI.WebControls.Button.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.WebControls {
  32. // CAS
  33. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. // attributes
  36. [DefaultEvent ("Click")]
  37. [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  38. [DefaultProperty ("Text")]
  39. [Designer ("System.Web.UI.Design.WebControls.ButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. #if NET_2_0
  41. [ToolboxDataAttribute ("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>")]
  42. [SupportsEventValidation]
  43. #else
  44. [ToolboxDataAttribute ("<{0}:Button runat=server Text=\"Button\"></{0}:Button>")]
  45. #endif
  46. public class Button : WebControl, IPostBackEventHandler
  47. #if NET_2_0
  48. , IButtonControl
  49. #endif
  50. {
  51. private static readonly object ClickEvent = new object ();
  52. private static readonly object CommandEvent = new object ();
  53. public Button () : base (HtmlTextWriterTag.Input)
  54. {
  55. }
  56. #if ONLY_1_1
  57. [Bindable (false)]
  58. #endif
  59. [WebSysDescription ("")]
  60. [WebCategory ("Behavior")]
  61. [DefaultValue (true)]
  62. #if NET_2_0
  63. [Themeable (false)]
  64. public virtual
  65. #else
  66. public
  67. #endif
  68. bool CausesValidation {
  69. get {
  70. return ViewState.GetBool ("CausesValidation", true);
  71. }
  72. set {
  73. ViewState ["CausesValidation"] = value;
  74. }
  75. }
  76. [DefaultValue ("")]
  77. [Bindable (true)]
  78. [WebSysDescription ("")]
  79. [WebCategory ("Behavior")]
  80. #if NET_2_0
  81. [Themeable (false)]
  82. #endif
  83. public string CommandArgument {
  84. get {
  85. return ViewState.GetString ("CommandArgument", "");
  86. }
  87. set {
  88. ViewState ["CommandArgument"] = value;
  89. }
  90. }
  91. [DefaultValue ("")]
  92. [WebSysDescription ("")]
  93. [WebCategory ("Behavior")]
  94. #if NET_2_0
  95. [Themeable (false)]
  96. #endif
  97. public string CommandName {
  98. get {
  99. return ViewState.GetString ("CommandName", "");
  100. }
  101. set {
  102. ViewState ["CommandName"] = value;
  103. }
  104. }
  105. #if NET_2_0
  106. [Themeable (false)]
  107. [DefaultValue ("")]
  108. [MonoTODO]
  109. [WebSysDescription ("")]
  110. [WebCategoryAttribute ("Behavior")]
  111. public virtual string OnClientClick
  112. {
  113. get {
  114. throw new NotImplementedException ();
  115. }
  116. set {
  117. throw new NotImplementedException ();
  118. }
  119. }
  120. #endif
  121. [DefaultValue ("")]
  122. [Bindable (true)]
  123. [WebSysDescription ("")]
  124. [WebCategory ("Appearance")]
  125. #if NET_2_0
  126. [Localizable (true)]
  127. #endif
  128. public string Text {
  129. get {
  130. return ViewState.GetString ("Text", "");
  131. }
  132. set {
  133. ViewState ["Text"] = value;
  134. }
  135. }
  136. #if NET_2_0
  137. [DefaultValue (true)]
  138. [Themeable (false)]
  139. [MonoTODO]
  140. [WebSysDescription ("")]
  141. [WebCategoryAttribute ("Behavior")]
  142. public virtual bool UseSubmitBehavior
  143. {
  144. get {
  145. throw new NotImplementedException ();
  146. }
  147. set {
  148. throw new NotImplementedException ();
  149. }
  150. }
  151. #endif
  152. protected override void AddAttributesToRender (HtmlTextWriter writer)
  153. {
  154. if (Page != null)
  155. Page.VerifyRenderingInServerForm (this);
  156. writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
  157. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  158. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  159. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  160. string onclick = Attributes["onclick"];
  161. if (onclick != null) {
  162. Attributes.Remove("onclick");
  163. int len = onclick.Length;
  164. if (len > 0 && onclick[len - 1] != ';')
  165. onclick += ";";
  166. }
  167. ClientScriptManager csm = new ClientScriptManager (Page);
  168. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
  169. writer.AddAttribute ("language", "javascript");
  170. }
  171. base.AddAttributesToRender (writer);
  172. }
  173. #if NET_2_0
  174. [MonoTODO]
  175. protected virtual PostBackOptions GetPostBackOptions ()
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. #endif
  180. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  181. {
  182. RaisePostBackEvent (eventArgument);
  183. }
  184. protected virtual void OnClick (EventArgs e)
  185. {
  186. if (Events != null) {
  187. EventHandler eh = (EventHandler) (Events [ClickEvent]);
  188. if (eh != null)
  189. eh (this, e);
  190. }
  191. }
  192. protected virtual void OnCommand (CommandEventArgs e)
  193. {
  194. if (Events != null) {
  195. CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
  196. if (eh != null)
  197. eh (this, e);
  198. }
  199. RaiseBubbleEvent (this, e);
  200. }
  201. #if NET_2_0
  202. protected virtual
  203. #endif
  204. void RaisePostBackEvent (string eventArgument)
  205. {
  206. if (CausesValidation)
  207. #if NET_2_0
  208. Page.Validate (ValidationGroup);
  209. #else
  210. Page.Validate ();
  211. #endif
  212. OnClick (EventArgs.Empty);
  213. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  214. }
  215. #if NET_2_0
  216. protected internal
  217. #else
  218. protected
  219. #endif
  220. override void RenderContents (HtmlTextWriter writer)
  221. {
  222. }
  223. [WebSysDescription ("")]
  224. [WebCategory ("Action")]
  225. public event EventHandler Click
  226. {
  227. add {
  228. Events.AddHandler (ClickEvent, value);
  229. }
  230. remove {
  231. Events.RemoveHandler (ClickEvent, value);
  232. }
  233. }
  234. [WebSysDescription ("")]
  235. [WebCategory ("Action")]
  236. public event CommandEventHandler Command
  237. {
  238. add {
  239. Events.AddHandler (CommandEvent, value);
  240. }
  241. remove {
  242. Events.RemoveHandler (CommandEvent, value);
  243. }
  244. }
  245. #if NET_2_0
  246. [DefaultValue ("")]
  247. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  248. [Themeable (false)]
  249. [UrlProperty("*.aspx")]
  250. [MonoTODO]
  251. public virtual string PostBackUrl {
  252. get {
  253. return ViewState.GetString ("PostBackUrl", "");
  254. }
  255. set {
  256. ViewState ["PostBackUrl"] = value;
  257. }
  258. }
  259. [DefaultValue ("")]
  260. [Themeable (false)]
  261. [WebSysDescription ("")]
  262. [WebCategoryAttribute ("Behavior")]
  263. public virtual string ValidationGroup {
  264. get {
  265. return ViewState.GetString ("ValidationGroup", "");
  266. }
  267. set {
  268. ViewState ["ValidationGroup"] = value;
  269. }
  270. }
  271. #endif
  272. }
  273. }