HtmlForm.cs 9.5 KB

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