HtmlForm.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // System.Web.UI.HtmlControls.HtmlForm.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.Collections.Specialized;
  30. using System.Security.Permissions;
  31. using System.Web.UI.WebControls;
  32. namespace System.Web.UI.HtmlControls
  33. {
  34. // CAS
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public class HtmlForm : HtmlContainerControl
  38. {
  39. public HtmlForm () : base ("form")
  40. {
  41. }
  42. #if NET_2_0
  43. string defaultbutton = "";
  44. [DefaultValue ("")]
  45. public string DefaultButton
  46. {
  47. get {
  48. return defaultbutton;
  49. }
  50. set {
  51. defaultbutton = (value == null ? "" : value);
  52. }
  53. }
  54. string defaultfocus = "";
  55. [DefaultValue ("")]
  56. public string DefaultFocus
  57. {
  58. get {
  59. return defaultfocus;
  60. }
  61. set {
  62. defaultfocus = (value == null ? "" : value);
  63. }
  64. }
  65. #endif
  66. [DefaultValue ("")]
  67. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  68. public string Enctype
  69. {
  70. get {
  71. string enc = Attributes["enctype"];
  72. if (enc == null) {
  73. return (String.Empty);
  74. }
  75. return (enc);
  76. }
  77. set {
  78. if (value == null) {
  79. Attributes.Remove ("enctype");
  80. } else {
  81. Attributes["enctype"] = value;
  82. }
  83. }
  84. }
  85. [DefaultValue ("")]
  86. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  87. public string Method
  88. {
  89. get {
  90. string method = Attributes["method"];
  91. if (method == null) {
  92. return ("post");
  93. }
  94. return (method);
  95. }
  96. set {
  97. if (value == null) {
  98. Attributes.Remove ("method");
  99. } else {
  100. Attributes["method"] = value;
  101. }
  102. }
  103. }
  104. [DefaultValue ("")]
  105. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  106. #if NET_2_0
  107. public virtual
  108. #else
  109. public
  110. #endif
  111. string Name
  112. {
  113. get {
  114. return UniqueID;
  115. }
  116. set {
  117. /* why am i here? I do nothing. */
  118. }
  119. }
  120. #if NET_2_0
  121. bool submitdisabledcontrols = false;
  122. [DefaultValue (false)]
  123. public virtual bool SubmitDisabledControls
  124. {
  125. get {
  126. return submitdisabledcontrols;
  127. }
  128. set {
  129. submitdisabledcontrols = value;
  130. }
  131. }
  132. #endif
  133. [DefaultValue ("")]
  134. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  135. public string Target
  136. {
  137. get {
  138. string target = Attributes["target"];
  139. if (target == null) {
  140. return (String.Empty);
  141. }
  142. return (target);
  143. }
  144. set {
  145. if (value == null) {
  146. Attributes.Remove ("target");
  147. } else {
  148. Attributes["target"] = value;
  149. }
  150. }
  151. }
  152. #if NET_2_0
  153. public override
  154. #else
  155. // New in NET1.1 sp1
  156. public new
  157. #endif
  158. string UniqueID
  159. {
  160. get {
  161. return base.UniqueID;
  162. }
  163. }
  164. #if NET_2_0
  165. [MonoTODO ("why override?")]
  166. protected override ControlCollection CreateControlCollection ()
  167. {
  168. return base.CreateControlCollection ();
  169. }
  170. #endif
  171. #if NET_2_0
  172. protected internal
  173. #else
  174. protected
  175. #endif
  176. override void OnInit (EventArgs e)
  177. {
  178. Page.RegisterViewStateHandler ();
  179. #if NET_2_0
  180. Page.RegisterForm (this);
  181. #endif
  182. base.OnInit (e);
  183. }
  184. #if NET_2_0
  185. internal bool DetermineRenderUplevel ()
  186. {
  187. /* this bit is c&p'ed from BaseValidator.DetermineRenderUplevel */
  188. try {
  189. if (Page != null && Page.Request != null)
  190. return (
  191. /* From someplace on the web: "JavaScript 1.2
  192. * and later (also known as ECMAScript) has
  193. * built-in support for regular
  194. * expressions" */
  195. ((Page.Request.Browser.EcmaScriptVersion.Major == 1
  196. && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
  197. || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
  198. /* document.getElementById, .getAttribute,
  199. * etc, are all DOM level 1. I don't think we
  200. * use anything in level 2.. */
  201. && Page.Request.Browser.W3CDomVersion.Major >= 1);
  202. }
  203. catch {
  204. /* this can happen with a fake Page in nunit
  205. * tests, since Page.Context == null */
  206. ;
  207. }
  208. return false;
  209. }
  210. protected internal override void OnPreRender (EventArgs e)
  211. {
  212. string focus_id = null;
  213. bool need_script_block = false;
  214. bool render_uplevel;
  215. base.OnPreRender(e);
  216. render_uplevel = DetermineRenderUplevel ();
  217. /* figure out if we have some control we're going to focus */
  218. if (DefaultFocus != null && DefaultFocus != "")
  219. focus_id = DefaultFocus;
  220. else if (DefaultButton != null && DefaultButton != "")
  221. focus_id = DefaultButton;
  222. /* decide if we need to include the script block */
  223. need_script_block = (focus_id != null || submitdisabledcontrols);
  224. if (render_uplevel) {
  225. Page.RequiresPostBackScript();
  226. if (need_script_block && !Page.ClientScript.IsClientScriptBlockRegistered ("Mono-System.Web-HtmlScriptBlock")) {
  227. Page.ClientScript.RegisterClientScriptBlock ("Mono-System.Web-HtmlScriptBlock",
  228. String.Format ("<script language=\"JavaScript\" src=\"{0}\"></script>",
  229. Page.ClientScript.GetWebResourceUrl (GetType(),
  230. "webform.js")));
  231. }
  232. if (focus_id != null) {
  233. Page.ClientScript.RegisterStartupScript ("HtmlForm-DefaultButton-StartupScript",
  234. String.Format ("<script language=\"JavaScript\">\n" +
  235. "<!--\n" +
  236. "WebForm_AutoFocus('{0}');// -->\n" +
  237. "</script>\n", focus_id));
  238. }
  239. if (submitdisabledcontrols) {
  240. Page.ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
  241. "javascript: return WebForm_OnSubmit();");
  242. Page.ClientScript.RegisterStartupScript ("HtmlForm-SubmitDisabledControls-StartupScript",
  243. @"<script language=""JavaScript"">
  244. <!--
  245. function WebForm_OnSubmit() {
  246. WebForm_ReEnableControls();
  247. return true;
  248. } // -->
  249. </script>");
  250. }
  251. }
  252. }
  253. #endif
  254. protected override void RenderAttributes (HtmlTextWriter w)
  255. {
  256. /* Need to always render: name, method, action
  257. * and id
  258. */
  259. string action = Page.Request.FilePath;
  260. string query = Page.Request.QueryStringRaw;
  261. if (query != null && query.Length > 0) {
  262. action += "?" + query;
  263. }
  264. w.WriteAttribute ("name", Name);
  265. w.WriteAttribute ("method", Method);
  266. w.WriteAttribute ("action", action);
  267. if (ID == null) {
  268. /* If ID != null then HtmlControl will
  269. * write the id attribute
  270. */
  271. w.WriteAttribute ("id", ClientID);
  272. Attributes.Remove ("id");
  273. }
  274. string submit = Page.GetSubmitStatements ();
  275. if (submit != null && submit != "")
  276. w.WriteAttribute ("onsubmit", submit);
  277. /* enctype and target should not be written if
  278. * they are empty
  279. */
  280. string enctype = Enctype;
  281. if (enctype != null && enctype != "") {
  282. w.WriteAttribute ("enctype", enctype);
  283. }
  284. string target = Target;
  285. if (target != null && target != "") {
  286. w.WriteAttribute ("target", target);
  287. }
  288. #if NET_2_0
  289. string defaultbutton = DefaultButton;
  290. if (defaultbutton != null && defaultbutton != "") {
  291. Control c = FindControl (defaultbutton);
  292. if (c == null || !(c is IButtonControl))
  293. throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
  294. ID));
  295. }
  296. #endif
  297. /* Now remove them from the hash so the base
  298. * RenderAttributes can do all the rest
  299. */
  300. Attributes.Remove ("method");
  301. Attributes.Remove ("enctype");
  302. Attributes.Remove ("target");
  303. base.RenderAttributes (w);
  304. }
  305. #if NET_2_0
  306. protected internal
  307. #else
  308. protected
  309. #endif
  310. override void RenderChildren (HtmlTextWriter w)
  311. {
  312. Page.OnFormRender (w, ClientID);
  313. base.RenderChildren (w);
  314. Page.OnFormPostRender (w, ClientID);
  315. }
  316. #if NET_2_0
  317. /* According to corcompare */
  318. [MonoTODO ("why override?")]
  319. public override void RenderControl (HtmlTextWriter w)
  320. {
  321. base.RenderControl (w);
  322. }
  323. #endif
  324. #if NET_2_0
  325. protected internal
  326. #else
  327. protected
  328. #endif
  329. override void Render (HtmlTextWriter w)
  330. {
  331. base.Render (w);
  332. }
  333. }
  334. }