HtmlInputButton.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // System.Web.UI.HtmlControls.HtmlInputButton.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005-2010 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.Globalization;
  30. using System.Reflection;
  31. using System.Security.Permissions;
  32. using System.Web.Util;
  33. namespace System.Web.UI.HtmlControls {
  34. // CAS
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [DefaultEventAttribute ("ServerClick")]
  39. [SupportsEventValidation]
  40. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler
  41. {
  42. static readonly object ServerClickEvent = new object();
  43. public HtmlInputButton () : this ("button")
  44. {
  45. }
  46. public HtmlInputButton (string type) : base (type)
  47. {
  48. }
  49. [DefaultValue(true)]
  50. [WebSysDescription("")]
  51. [WebCategory("Behavior")]
  52. public virtual bool CausesValidation {
  53. get {
  54. string flag = Attributes["CausesValidation"];
  55. if (flag == null)
  56. return true;
  57. return Boolean.Parse (flag);
  58. }
  59. set {
  60. Attributes ["CausesValidation"] = value.ToString();
  61. }
  62. }
  63. [DefaultValue ("")]
  64. public virtual string ValidationGroup
  65. {
  66. get {
  67. string group = Attributes["ValidationGroup"];
  68. if (group == null)
  69. return "";
  70. return group;
  71. }
  72. set {
  73. if (value == null)
  74. Attributes.Remove ("ValidationGroup");
  75. else
  76. Attributes["ValidationGroup"] = value;
  77. }
  78. }
  79. void RaisePostBackEventInternal (string eventArgument)
  80. {
  81. ValidateEvent (UniqueID, eventArgument);
  82. if (CausesValidation)
  83. Page.Validate (ValidationGroup);
  84. if (String.Compare (Type, "reset", true, Helpers.InvariantCulture) != 0)
  85. OnServerClick (EventArgs.Empty);
  86. else
  87. ResetForm (FindForm ());
  88. }
  89. HtmlForm FindForm ()
  90. {
  91. Page p = Page;
  92. if (p != null)
  93. return p.Form;
  94. return null;
  95. }
  96. void ResetForm (HtmlForm form)
  97. {
  98. if (form == null || !form.HasControls ())
  99. return;
  100. ResetChildrenValues (form.Controls);
  101. }
  102. void ResetChildrenValues (ControlCollection children)
  103. {
  104. foreach (Control child in children) {
  105. if (child == null)
  106. continue;
  107. if (child.HasControls ())
  108. ResetChildrenValues (child.Controls);
  109. ResetChildValue (child);
  110. }
  111. }
  112. void ResetChildValue (Control child)
  113. {
  114. Type type = child.GetType ();
  115. object[] attributes = type.GetCustomAttributes (false);
  116. if (attributes == null || attributes.Length == 0)
  117. return;
  118. string defaultProperty = null;
  119. DefaultPropertyAttribute defprop;
  120. foreach (object attr in attributes) {
  121. defprop = attr as DefaultPropertyAttribute;
  122. if (defprop == null)
  123. continue;
  124. defaultProperty = defprop.Name;
  125. break;
  126. }
  127. if (defaultProperty == null || defaultProperty.Length == 0)
  128. return;
  129. PropertyInfo pi = null;
  130. try {
  131. pi = type.GetProperty (defaultProperty,
  132. BindingFlags.Instance |
  133. BindingFlags.Public |
  134. BindingFlags.Static |
  135. BindingFlags.IgnoreCase);
  136. } catch (Exception) {
  137. // ignore
  138. }
  139. if (pi == null || !pi.CanWrite)
  140. return;
  141. attributes = pi.GetCustomAttributes (false);
  142. if (attributes == null || attributes.Length == 0)
  143. return;
  144. DefaultValueAttribute defval = null;
  145. object value = null;
  146. foreach (object attr in attributes) {
  147. defval = attr as DefaultValueAttribute;
  148. if (defval == null)
  149. continue;
  150. value = defval.Value;
  151. break;
  152. }
  153. if (value == null || pi.PropertyType != value.GetType ())
  154. return;
  155. try {
  156. pi.SetValue (child, value, null);
  157. } catch (Exception) {
  158. // ignore
  159. }
  160. }
  161. protected virtual void RaisePostBackEvent (string eventArgument)
  162. {
  163. RaisePostBackEventInternal (eventArgument);
  164. }
  165. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  166. {
  167. RaisePostBackEvent (eventArgument);
  168. }
  169. protected internal override void OnPreRender (EventArgs e)
  170. {
  171. base.OnPreRender (e);
  172. if (Events [ServerClickEvent] != null)
  173. Page.RequiresPostBackScript ();
  174. }
  175. protected virtual void OnServerClick (EventArgs e)
  176. {
  177. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  178. if (server_click != null)
  179. server_click (this, e);
  180. }
  181. protected override void RenderAttributes (HtmlTextWriter writer)
  182. {
  183. CultureInfo inv = Helpers.InvariantCulture;
  184. string input_type = Type;
  185. if (0 != String.Compare (input_type, "reset", true, inv) &&
  186. ((0 == String.Compare (input_type, "submit", true, inv)) ||
  187. (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
  188. string onclick = String.Empty;
  189. if (Attributes ["onclick"] != null) {
  190. onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
  191. Attributes.Remove ("onclick");
  192. }
  193. Page page = Page;
  194. if (page != null) {
  195. PostBackOptions options = GetPostBackOptions ();
  196. onclick += page.ClientScript.GetPostBackEventReference (options, true);
  197. }
  198. if (onclick.Length > 0) {
  199. bool encode = true;
  200. if (Events [ServerClickEvent] != null)
  201. encode = false; // tests show that this is indeed
  202. // the case...
  203. writer.WriteAttribute ("onclick", onclick, encode);
  204. writer.WriteAttribute ("language", "javascript");
  205. }
  206. }
  207. Attributes.Remove ("CausesValidation");
  208. // LAMESPEC: MS doesn't actually remove this
  209. //attribute. it shows up in the rendered
  210. //output.
  211. // Attributes.Remove("ValidationGroup");
  212. base.RenderAttributes (writer);
  213. }
  214. PostBackOptions GetPostBackOptions ()
  215. {
  216. Page page = Page;
  217. PostBackOptions options = new PostBackOptions (this);
  218. options.ValidationGroup = null;
  219. options.ActionUrl = null;
  220. options.Argument = String.Empty;
  221. options.RequiresJavaScriptProtocol = false;
  222. options.ClientSubmit = (0 != String.Compare (Type, "submit", true, Helpers.InvariantCulture));
  223. options.PerformValidation = CausesValidation && page != null && page.Validators.Count > 0;
  224. if (options.PerformValidation)
  225. options.ValidationGroup = ValidationGroup;
  226. return options;
  227. }
  228. [WebSysDescription("")]
  229. [WebCategory("Action")]
  230. public event EventHandler ServerClick {
  231. add { Events.AddHandler (ServerClickEvent, value); }
  232. remove { Events.RemoveHandler (ServerClickEvent, value); }
  233. }
  234. }
  235. }