2
0

HtmlInputButton.cs 8.6 KB

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