HtmlForm.cs 9.6 KB

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