HtmlInputButton.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputButton.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc.
  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.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. [DefaultEventAttribute ("ServerClick")]
  36. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
  37. private static readonly object ServerClickEvent = new object();
  38. public HtmlInputButton () : this ("button")
  39. {
  40. }
  41. public HtmlInputButton (string type) : base (type)
  42. {
  43. }
  44. [DefaultValue(true)]
  45. [WebSysDescription("")]
  46. [WebCategory("Behavior")]
  47. #if NET_2_0
  48. public virtual
  49. #else
  50. public
  51. #endif
  52. bool CausesValidation {
  53. get {
  54. return ViewState.GetBool ("CausesValidation", true);
  55. }
  56. set {
  57. ViewState ["CausesValidation"] = value;
  58. }
  59. }
  60. #if NET_2_0
  61. [DefaultValue ("")]
  62. public string ValidationGroup
  63. {
  64. get {
  65. return ViewState.GetString ("ValidationGroup", "");
  66. }
  67. set {
  68. ViewState ["ValidationGroup"] = value;
  69. }
  70. }
  71. #endif
  72. void RaisePostBackEventInternal (string eventArgument)
  73. {
  74. if (CausesValidation)
  75. #if NET_2_0
  76. Page.Validate (ValidationGroup);
  77. #else
  78. Page.Validate ();
  79. #endif
  80. OnServerClick (EventArgs.Empty);
  81. }
  82. #if NET_2_0
  83. protected virtual void RaisePostBackEvent (string eventArgument)
  84. {
  85. RaisePostBackEventInternal (eventArgument);
  86. }
  87. #endif
  88. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  89. {
  90. #if NET_2_0
  91. RaisePostBackEvent (eventArgument);
  92. #else
  93. RaisePostBackEventInternal (eventArgument);
  94. #endif
  95. }
  96. #if NET_2_0
  97. protected internal
  98. #else
  99. protected
  100. #endif
  101. override void OnPreRender (EventArgs e)
  102. {
  103. base.OnPreRender (e);
  104. if (Events [ServerClickEvent] != null)
  105. Page.RequiresPostBackScript ();
  106. }
  107. protected virtual void OnServerClick (EventArgs e)
  108. {
  109. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  110. if (server_click != null)
  111. server_click (this, e);
  112. }
  113. protected override void RenderAttributes (HtmlTextWriter writer)
  114. {
  115. if (CausesValidation && Page != null && Page.AreValidatorsUplevel ()) {
  116. ClientScriptManager csm = new ClientScriptManager (Page);
  117. writer.WriteAttribute ("onclick", csm.GetClientValidationEvent ());
  118. writer.WriteAttribute ("language", "javascript");
  119. }
  120. base.RenderAttributes (writer);
  121. }
  122. [WebSysDescription("")]
  123. [WebCategory("Action")]
  124. public event EventHandler ServerClick {
  125. add { Events.AddHandler (ServerClickEvent, value); }
  126. remove { Events.RemoveHandler (ServerClickEvent, value); }
  127. }
  128. }
  129. }