HtmlInputButton.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.Globalization;
  30. using System.Reflection;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.HtmlControls {
  33. // CAS
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [DefaultEventAttribute ("ServerClick")]
  38. #if NET_2_0
  39. [SupportsEventValidation]
  40. #endif
  41. public class HtmlInputButton : HtmlInputControl, IPostBackEventHandler {
  42. private 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. #if NET_2_0
  53. public virtual
  54. #else
  55. public
  56. #endif
  57. bool CausesValidation {
  58. get {
  59. string flag = Attributes["CausesValidation"];
  60. if (flag == null)
  61. return true;
  62. return Boolean.Parse (flag);
  63. }
  64. set {
  65. Attributes ["CausesValidation"] = value.ToString();
  66. }
  67. }
  68. #if NET_2_0
  69. [DefaultValue ("")]
  70. public virtual string ValidationGroup
  71. {
  72. get {
  73. string group = Attributes["ValidationGroup"];
  74. if (group == null)
  75. return "";
  76. return group;
  77. }
  78. set {
  79. if (value == null)
  80. Attributes.Remove ("ValidationGroup");
  81. else
  82. Attributes["ValidationGroup"] = value;
  83. }
  84. }
  85. #endif
  86. void RaisePostBackEventInternal (string eventArgument)
  87. {
  88. if (CausesValidation) {
  89. #if NET_2_0
  90. Page.Validate (ValidationGroup);
  91. #else
  92. Page.Validate ();
  93. #endif
  94. }
  95. if (String.Compare (Type, "reset", true, CultureInfo.InvariantCulture) != 0)
  96. OnServerClick (EventArgs.Empty);
  97. else
  98. ResetForm (FindForm ());
  99. }
  100. HtmlForm FindForm ()
  101. {
  102. #if NET_2_0
  103. return Page.Form;
  104. #else
  105. HtmlForm ret = null;
  106. Control p = Parent;
  107. while (p != null) {
  108. ret = p as HtmlForm;
  109. if (ret == null) {
  110. p = p.Parent;
  111. continue;
  112. }
  113. return ret;
  114. }
  115. return null;
  116. #endif
  117. }
  118. void ResetForm (HtmlForm form)
  119. {
  120. if (form == null || !form.HasControls ())
  121. return;
  122. ResetChildrenValues (form.Controls);
  123. }
  124. void ResetChildrenValues (ControlCollection children)
  125. {
  126. foreach (Control child in children) {
  127. if (child == null)
  128. continue;
  129. if (child.HasControls ())
  130. ResetChildrenValues (child.Controls);
  131. ResetChildValue (child);
  132. }
  133. }
  134. void ResetChildValue (Control child)
  135. {
  136. Type type = child.GetType ();
  137. object[] attributes = type.GetCustomAttributes (false);
  138. if (attributes == null || attributes.Length == 0)
  139. return;
  140. string defaultProperty = null;
  141. DefaultPropertyAttribute defprop;
  142. foreach (object attr in attributes) {
  143. defprop = attr as DefaultPropertyAttribute;
  144. if (defprop == null)
  145. continue;
  146. defaultProperty = defprop.Name;
  147. break;
  148. }
  149. if (defaultProperty == null || defaultProperty.Length == 0)
  150. return;
  151. PropertyInfo pi = null;
  152. try {
  153. pi = type.GetProperty (defaultProperty,
  154. BindingFlags.Instance |
  155. BindingFlags.Public |
  156. BindingFlags.Static |
  157. BindingFlags.IgnoreCase);
  158. } catch (Exception) {
  159. // ignore
  160. }
  161. if (pi == null || !pi.CanWrite)
  162. return;
  163. attributes = pi.GetCustomAttributes (false);
  164. if (attributes == null || attributes.Length == 0)
  165. return;
  166. DefaultValueAttribute defval = null;
  167. object value = null;
  168. foreach (object attr in attributes) {
  169. defval = attr as DefaultValueAttribute;
  170. if (defval == null)
  171. continue;
  172. value = defval.Value;
  173. break;
  174. }
  175. if (value == null || pi.PropertyType != value.GetType ())
  176. return;
  177. try {
  178. pi.SetValue (child, value, null);
  179. } catch (Exception) {
  180. // ignore
  181. }
  182. }
  183. #if NET_2_0
  184. protected virtual void RaisePostBackEvent (string eventArgument)
  185. {
  186. RaisePostBackEventInternal (eventArgument);
  187. }
  188. #endif
  189. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  190. {
  191. #if NET_2_0
  192. RaisePostBackEvent (eventArgument);
  193. #else
  194. RaisePostBackEventInternal (eventArgument);
  195. #endif
  196. }
  197. #if NET_2_0
  198. protected internal
  199. #else
  200. protected
  201. #endif
  202. override void OnPreRender (EventArgs e)
  203. {
  204. base.OnPreRender (e);
  205. if (Events [ServerClickEvent] != null)
  206. Page.RequiresPostBackScript ();
  207. }
  208. protected virtual void OnServerClick (EventArgs e)
  209. {
  210. EventHandler server_click = (EventHandler) Events [ServerClickEvent];
  211. if (server_click != null)
  212. server_click (this, e);
  213. }
  214. #if !NET_2_0
  215. bool RenderOnClick ()
  216. {
  217. if (Page == null || !CausesValidation)
  218. return false;
  219. CultureInfo inv = CultureInfo.InvariantCulture;
  220. string input_type = Type;
  221. if (0 == String.Compare (input_type, "submit", true, inv) &&
  222. Page.Validators.Count > 0)
  223. return true;
  224. if (0 == String.Compare (input_type, "button", true, inv) &&
  225. Events [ServerClickEvent] != null)
  226. return true;
  227. return false;
  228. }
  229. #endif
  230. protected override void RenderAttributes (HtmlTextWriter writer)
  231. {
  232. #if NET_2_0
  233. CultureInfo inv = CultureInfo.InvariantCulture;
  234. string input_type = Type;
  235. if (0 != String.Compare (input_type, "reset", true, inv) &&
  236. ((0 == String.Compare (input_type, "submit", true, inv)) ||
  237. (0 == String.Compare (input_type, "button", true, inv) && Events [ServerClickEvent] != null))) {
  238. string onclick = String.Empty;
  239. if (Attributes ["onclick"] != null) {
  240. onclick = ClientScriptManager.EnsureEndsWithSemicolon (Attributes ["onclick"] + onclick);
  241. Attributes.Remove ("onclick");
  242. }
  243. if (Page != null) {
  244. PostBackOptions options = GetPostBackOptions ();
  245. Page.ClientScript.RegisterForEventValidation (options);
  246. onclick += Page.ClientScript.GetPostBackEventReference (options);
  247. }
  248. if (onclick.Length > 0) {
  249. writer.WriteAttribute ("onclick", onclick);
  250. writer.WriteAttribute ("language", "javascript");
  251. }
  252. }
  253. #else
  254. if (RenderOnClick ()) {
  255. string oc = null;
  256. ClientScriptManager csm = new ClientScriptManager (Page);
  257. if (Page.AreValidatorsUplevel ()) {
  258. oc = csm.GetClientValidationEvent ();
  259. } else if (Events [ServerClickEvent] != null) {
  260. oc = Attributes ["onclick"] + " " + csm.GetPostBackEventReference (this, "");
  261. }
  262. if (oc != null) {
  263. writer.WriteAttribute ("language", "javascript");
  264. writer.WriteAttribute ("onclick", oc);
  265. }
  266. }
  267. #endif
  268. Attributes.Remove ("CausesValidation");
  269. #if NET_2_0
  270. // LAMESPEC: MS doesn't actually remove this
  271. //attribute. it shows up in the rendered
  272. //output.
  273. // Attributes.Remove("ValidationGroup");
  274. #endif
  275. base.RenderAttributes (writer);
  276. }
  277. #if NET_2_0
  278. PostBackOptions GetPostBackOptions () {
  279. PostBackOptions options = new PostBackOptions (this);
  280. options.ValidationGroup = null;
  281. options.ActionUrl = null;
  282. options.Argument = "";
  283. options.RequiresJavaScriptProtocol = false;
  284. options.ClientSubmit = (0 != String.Compare (Type, "submit", true, CultureInfo.InvariantCulture));
  285. options.PerformValidation = CausesValidation && Page != null && Page.Validators.Count > 0;
  286. if (options.PerformValidation)
  287. options.ValidationGroup = ValidationGroup;
  288. return options;
  289. }
  290. #endif
  291. [WebSysDescription("")]
  292. [WebCategory("Action")]
  293. public event EventHandler ServerClick {
  294. add { Events.AddHandler (ServerClickEvent, value); }
  295. remove { Events.RemoveHandler (ServerClickEvent, value); }
  296. }
  297. }
  298. }