HtmlInputButton.cs 8.6 KB

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