Button.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #if NET_2_0
  158. if (ID != null)
  159. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  160. #else
  161. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  162. #endif
  163. writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
  164. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  165. string onclick = Attributes["onclick"];
  166. if (onclick != null) {
  167. Attributes.Remove("onclick");
  168. int len = onclick.Length;
  169. if (len > 0 && onclick[len - 1] != ';')
  170. onclick += ";";
  171. }
  172. ClientScriptManager csm = new ClientScriptManager (Page);
  173. writer.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick + csm.GetClientValidationEvent ());
  174. writer.AddAttribute ("language", "javascript");
  175. }
  176. base.AddAttributesToRender (writer);
  177. }
  178. #if NET_2_0
  179. [MonoTODO]
  180. protected virtual PostBackOptions GetPostBackOptions ()
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. #endif
  185. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  186. {
  187. RaisePostBackEvent (eventArgument);
  188. }
  189. protected virtual void OnClick (EventArgs e)
  190. {
  191. if (Events != null) {
  192. EventHandler eh = (EventHandler) (Events [ClickEvent]);
  193. if (eh != null)
  194. eh (this, e);
  195. }
  196. }
  197. protected virtual void OnCommand (CommandEventArgs e)
  198. {
  199. if (Events != null) {
  200. CommandEventHandler eh = (CommandEventHandler) (Events [CommandEvent]);
  201. if (eh != null)
  202. eh (this, e);
  203. }
  204. RaiseBubbleEvent (this, e);
  205. }
  206. #if NET_2_0
  207. protected virtual
  208. #endif
  209. void RaisePostBackEvent (string eventArgument)
  210. {
  211. if (CausesValidation)
  212. #if NET_2_0
  213. Page.Validate (ValidationGroup);
  214. #else
  215. Page.Validate ();
  216. #endif
  217. OnClick (EventArgs.Empty);
  218. OnCommand (new CommandEventArgs (CommandName, CommandArgument));
  219. }
  220. #if NET_2_0
  221. protected internal
  222. #else
  223. protected
  224. #endif
  225. override void RenderContents (HtmlTextWriter writer)
  226. {
  227. }
  228. [WebSysDescription ("")]
  229. [WebCategory ("Action")]
  230. public event EventHandler Click
  231. {
  232. add {
  233. Events.AddHandler (ClickEvent, value);
  234. }
  235. remove {
  236. Events.RemoveHandler (ClickEvent, value);
  237. }
  238. }
  239. [WebSysDescription ("")]
  240. [WebCategory ("Action")]
  241. public event CommandEventHandler Command
  242. {
  243. add {
  244. Events.AddHandler (CommandEvent, value);
  245. }
  246. remove {
  247. Events.RemoveHandler (CommandEvent, value);
  248. }
  249. }
  250. #if NET_2_0
  251. [DefaultValue ("")]
  252. [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  253. [Themeable (false)]
  254. [UrlProperty("*.aspx")]
  255. [MonoTODO]
  256. public virtual string PostBackUrl {
  257. get {
  258. throw new NotImplementedException ();
  259. }
  260. set {
  261. throw new NotImplementedException ();
  262. }
  263. }
  264. [DefaultValue ("")]
  265. [Themeable (false)]
  266. [WebSysDescription ("")]
  267. [WebCategoryAttribute ("Behavior")]
  268. public virtual string ValidationGroup {
  269. get {
  270. return ViewState.GetString ("ValidationGroup", "");
  271. }
  272. set {
  273. ViewState ["ValidationGroup"] = value;
  274. }
  275. }
  276. #endif
  277. }
  278. }