HtmlForm.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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) || (method.Length == 0)) {
  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. public virtual string Name
  109. {
  110. get {
  111. return UniqueID;
  112. }
  113. set {
  114. /* why am i here? I do nothing. */
  115. }
  116. }
  117. #if NET_2_0
  118. bool submitdisabledcontrols = false;
  119. [DefaultValue (false)]
  120. public virtual bool SubmitDisabledControls
  121. {
  122. get {
  123. return submitdisabledcontrols;
  124. }
  125. set {
  126. submitdisabledcontrols = value;
  127. }
  128. }
  129. #endif
  130. [DefaultValue ("")]
  131. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  132. public string Target
  133. {
  134. get {
  135. string target = Attributes["target"];
  136. if (target == null) {
  137. return (String.Empty);
  138. }
  139. return (target);
  140. }
  141. set {
  142. if (value == null) {
  143. Attributes.Remove ("target");
  144. } else {
  145. Attributes["target"] = value;
  146. }
  147. }
  148. }
  149. public override string UniqueID {
  150. get {
  151. return base.UniqueID;
  152. }
  153. }
  154. #if NET_2_0
  155. [MonoTODO ("why override?")]
  156. protected override ControlCollection CreateControlCollection ()
  157. {
  158. return base.CreateControlCollection ();
  159. }
  160. #endif
  161. #if NET_2_0
  162. protected internal
  163. #else
  164. protected
  165. #endif
  166. override void OnInit (EventArgs e)
  167. {
  168. inited = true;
  169. Page.RegisterViewStateHandler ();
  170. #if NET_2_0
  171. Page.RegisterForm (this);
  172. #endif
  173. base.OnInit (e);
  174. }
  175. #if NET_2_0
  176. internal bool DetermineRenderUplevel ()
  177. {
  178. /* this bit is c&p'ed from BaseValidator.DetermineRenderUplevel */
  179. try {
  180. if (Page != null && Page.Request != null)
  181. return (
  182. /* From someplace on the web: "JavaScript 1.2
  183. * and later (also known as ECMAScript) has
  184. * built-in support for regular
  185. * expressions" */
  186. ((Page.Request.Browser.EcmaScriptVersion.Major == 1
  187. && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
  188. || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
  189. /* document.getElementById, .getAttribute,
  190. * etc, are all DOM level 1. I don't think we
  191. * use anything in level 2.. */
  192. && Page.Request.Browser.W3CDomVersion.Major >= 1);
  193. }
  194. catch {
  195. /* this can happen with a fake Page in nunit
  196. * tests, since Page.Context == null */
  197. ;
  198. }
  199. return false;
  200. }
  201. protected internal override void OnPreRender (EventArgs e)
  202. {
  203. string focus_id = null;
  204. bool need_script_block = false;
  205. bool render_uplevel;
  206. base.OnPreRender(e);
  207. render_uplevel = DetermineRenderUplevel ();
  208. /* figure out if we have some control we're going to focus */
  209. if (DefaultFocus != null && DefaultFocus != "")
  210. focus_id = DefaultFocus;
  211. else if (DefaultButton != null && DefaultButton != "")
  212. focus_id = DefaultButton;
  213. /* decide if we need to include the script block */
  214. need_script_block = (focus_id != null || submitdisabledcontrols);
  215. if (render_uplevel) {
  216. Page.RequiresPostBackScript();
  217. if (need_script_block && !Page.ClientScript.IsClientScriptBlockRegistered ("Mono-System.Web-HtmlScriptBlock")) {
  218. Page.ClientScript.RegisterClientScriptBlock ("Mono-System.Web-HtmlScriptBlock",
  219. String.Format ("<script language=\"JavaScript\" src=\"{0}\"></script>",
  220. Page.ClientScript.GetWebResourceUrl (GetType(),
  221. "webform.js")));
  222. }
  223. if (focus_id != null) {
  224. Page.ClientScript.RegisterStartupScript ("HtmlForm-DefaultButton-StartupScript",
  225. String.Format ("<script language=\"JavaScript\">\n" +
  226. "<!--\n" +
  227. "WebForm_AutoFocus('{0}');// -->\n" +
  228. "</script>\n", focus_id));
  229. }
  230. if (submitdisabledcontrols) {
  231. Page.ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
  232. "javascript: return WebForm_OnSubmit();");
  233. Page.ClientScript.RegisterStartupScript ("HtmlForm-SubmitDisabledControls-StartupScript",
  234. @"<script language=""JavaScript"">
  235. <!--
  236. function WebForm_OnSubmit() {
  237. WebForm_ReEnableControls();
  238. return true;
  239. } // -->
  240. </script>");
  241. }
  242. }
  243. }
  244. #endif
  245. protected override void RenderAttributes (HtmlTextWriter w)
  246. {
  247. /* Need to always render: name, method, action
  248. * and id
  249. */
  250. string action;
  251. string file_path = Page.Request.FilePath;
  252. string current_path = Page.Request.CurrentExecutionFilePath;
  253. if (file_path == current_path) {
  254. // Just the filename will do
  255. action = UrlUtils.GetFile (file_path);
  256. } else {
  257. // Fun. We need to make cookieless sessions work, so no
  258. // absolute paths here.
  259. Uri current_uri = new Uri ("http://host" + current_path);
  260. Uri fp_uri = new Uri ("http://host" + file_path);
  261. action = fp_uri.MakeRelative (current_uri);
  262. }
  263. action += Page.Request.QueryStringRaw;
  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. if (!inited) {
  313. Page.RegisterViewStateHandler ();
  314. #if NET_2_0
  315. Page.RegisterForm (this);
  316. #endif
  317. }
  318. Page.OnFormRender (w, ClientID);
  319. base.RenderChildren (w);
  320. Page.OnFormPostRender (w, ClientID);
  321. }
  322. #if NET_2_0
  323. /* According to corcompare */
  324. [MonoTODO ("why override?")]
  325. public override void RenderControl (HtmlTextWriter w)
  326. {
  327. base.RenderControl (w);
  328. }
  329. #endif
  330. #if NET_2_0
  331. protected internal
  332. #else
  333. protected
  334. #endif
  335. override void Render (HtmlTextWriter w)
  336. {
  337. base.Render (w);
  338. }
  339. }
  340. }