HtmlButton.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. //
  22. // System.Web.UI.HtmlControls.HtmlButton
  23. //
  24. // Authors:
  25. // Jackson Harper ([email protected])
  26. //
  27. // (C) 2005-2010 Novell, Inc.
  28. using System.ComponentModel;
  29. using System.Security.Permissions;
  30. namespace System.Web.UI.HtmlControls {
  31. // CAS
  32. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  33. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. // attributes
  35. [DefaultEvent("ServerClick")]
  36. [SupportsEventValidation]
  37. public class HtmlButton : HtmlContainerControl, IPostBackEventHandler {
  38. static readonly object ServerClickEvent = new object();
  39. public HtmlButton () : base ("button")
  40. {
  41. }
  42. [DefaultValue(true)]
  43. [WebSysDescription("")]
  44. [WebCategory("Behavior")]
  45. public virtual bool CausesValidation {
  46. get {
  47. return ViewState.GetBool ("CausesValidation", true);
  48. }
  49. set {
  50. ViewState ["CausesValidation"] = value;
  51. }
  52. }
  53. [DefaultValue ("")]
  54. public virtual string ValidationGroup
  55. {
  56. get {
  57. return ViewState.GetString ("ValidationGroup", "");
  58. }
  59. set {
  60. ViewState ["ValidationGroup"] = value;
  61. }
  62. }
  63. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  64. {
  65. RaisePostBackEvent (eventArgument);
  66. }
  67. protected virtual void RaisePostBackEvent (string eventArgument)
  68. {
  69. ValidateEvent (UniqueID, eventArgument);
  70. if (CausesValidation)
  71. Page.Validate (ValidationGroup);
  72. OnServerClick (EventArgs.Empty);
  73. }
  74. protected internal override void OnPreRender (EventArgs e)
  75. {
  76. base.OnPreRender (e);
  77. }
  78. protected virtual void OnServerClick (EventArgs e)
  79. {
  80. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  81. if (server_click != null)
  82. server_click (this, e);
  83. }
  84. protected override void RenderAttributes (HtmlTextWriter writer)
  85. {
  86. Page page = Page;
  87. if (page != null && Events [ServerClickEvent] != null) {
  88. PostBackOptions options = GetPostBackOptions ();
  89. Attributes ["onclick"] += page.ClientScript.GetPostBackEventReference (options, true);
  90. writer.WriteAttribute ("language", "javascript");
  91. }
  92. base.RenderAttributes (writer);
  93. }
  94. PostBackOptions GetPostBackOptions ()
  95. {
  96. Page page = Page;
  97. PostBackOptions options = new PostBackOptions (this);
  98. options.ValidationGroup = null;
  99. options.ActionUrl = null;
  100. options.Argument = String.Empty;
  101. options.RequiresJavaScriptProtocol = false;
  102. options.ClientSubmit = true;
  103. options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
  104. if (options.PerformValidation)
  105. options.ValidationGroup = ValidationGroup;
  106. return options;
  107. }
  108. [WebSysDescription("")]
  109. [WebCategory("Action")]
  110. public event EventHandler ServerClick {
  111. add { Events.AddHandler (ServerClickEvent, value); }
  112. remove { Events.RemoveHandler (ServerClickEvent, value); }
  113. }
  114. }
  115. }