HtmlForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // System.Web.UI.HtmlControls.HtmlForm.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (C) 2005-2009 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. using System.Web.Configuration;
  34. using System.Web.SessionState;
  35. namespace System.Web.UI.HtmlControls
  36. {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. public class HtmlForm : HtmlContainerControl
  41. {
  42. bool inited;
  43. public HtmlForm () : base ("form")
  44. {
  45. }
  46. #if NET_2_0
  47. // LAMESPEC: This is undocumented on MSDN, but apparently it does exist on MS.NET.
  48. // See https://bugzilla.novell.com/show_bug.cgi?id=442104
  49. public string Action {
  50. get {
  51. string action = Attributes ["action"];
  52. if (String.IsNullOrEmpty (action))
  53. return String.Empty;
  54. return action;
  55. }
  56. set {
  57. if (String.IsNullOrEmpty (value))
  58. Attributes ["action"] = null;
  59. else
  60. Attributes ["action"] = value;
  61. }
  62. }
  63. string _defaultbutton;
  64. [DefaultValue ("")]
  65. public string DefaultButton
  66. {
  67. get {
  68. return _defaultbutton ?? String.Empty;
  69. }
  70. set {
  71. _defaultbutton = value;
  72. }
  73. }
  74. string _defaultfocus;
  75. [DefaultValue ("")]
  76. public string DefaultFocus
  77. {
  78. get {
  79. return _defaultfocus ?? String.Empty;
  80. }
  81. set {
  82. _defaultfocus = value;
  83. }
  84. }
  85. #endif
  86. [DefaultValue ("")]
  87. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  88. public string Enctype
  89. {
  90. get {
  91. string enc = Attributes["enctype"];
  92. if (enc == null) {
  93. return (String.Empty);
  94. }
  95. return (enc);
  96. }
  97. set {
  98. if (value == null) {
  99. Attributes.Remove ("enctype");
  100. } else {
  101. Attributes["enctype"] = value;
  102. }
  103. }
  104. }
  105. [DefaultValue ("")]
  106. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  107. public string Method
  108. {
  109. get {
  110. string method = Attributes["method"];
  111. if ((method == null) || (method.Length == 0)) {
  112. return ("post");
  113. }
  114. return (method);
  115. }
  116. set {
  117. if (value == null) {
  118. Attributes.Remove ("method");
  119. } else {
  120. Attributes["method"] = value;
  121. }
  122. }
  123. }
  124. [DefaultValue ("")]
  125. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  126. public virtual string Name
  127. {
  128. get {
  129. return UniqueID;
  130. }
  131. set {
  132. /* why am i here? I do nothing. */
  133. }
  134. }
  135. #if NET_2_0
  136. bool submitdisabledcontrols = false;
  137. [DefaultValue (false)]
  138. public virtual bool SubmitDisabledControls
  139. {
  140. get {
  141. return submitdisabledcontrols;
  142. }
  143. set {
  144. submitdisabledcontrols = value;
  145. }
  146. }
  147. #endif
  148. [DefaultValue ("")]
  149. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  150. public string Target
  151. {
  152. get {
  153. string target = Attributes["target"];
  154. if (target == null) {
  155. return (String.Empty);
  156. }
  157. return (target);
  158. }
  159. set {
  160. if (value == null) {
  161. Attributes.Remove ("target");
  162. } else {
  163. Attributes["target"] = value;
  164. }
  165. }
  166. }
  167. public override string UniqueID {
  168. get {
  169. return base.UniqueID;
  170. }
  171. }
  172. #if NET_2_0
  173. [MonoTODO ("why override?")]
  174. protected override ControlCollection CreateControlCollection ()
  175. {
  176. return base.CreateControlCollection ();
  177. }
  178. #endif
  179. #if NET_2_0
  180. protected internal
  181. #else
  182. protected
  183. #endif
  184. override void OnInit (EventArgs e)
  185. {
  186. inited = true;
  187. Page.RegisterViewStateHandler ();
  188. #if NET_2_0
  189. Page.RegisterForm (this);
  190. #endif
  191. base.OnInit (e);
  192. }
  193. #if NET_2_0
  194. bool? isUplevel;
  195. internal bool DetermineRenderUplevel ()
  196. {
  197. #if TARGET_J2EE
  198. if (HttpContext.Current == null)
  199. return false;
  200. return (
  201. /* From someplace on the web: "JavaScript 1.2
  202. * and later (also known as ECMAScript) has
  203. * built-in support for regular
  204. * expressions" */
  205. ((Page.Request.Browser.EcmaScriptVersion.Major == 1
  206. && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
  207. || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
  208. /* document.getElementById, .getAttribute,
  209. * etc, are all DOM level 1. I don't think we
  210. * use anything in level 2.. */
  211. && Page.Request.Browser.W3CDomVersion.Major >= 1);
  212. #else
  213. if (isUplevel != null)
  214. return (bool) isUplevel;
  215. isUplevel = UplevelHelper.IsUplevel (
  216. System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
  217. return (bool) isUplevel;
  218. #endif
  219. }
  220. protected internal override void OnPreRender (EventArgs e)
  221. {
  222. base.OnPreRender(e);
  223. }
  224. #endif
  225. protected override void RenderAttributes (HtmlTextWriter w)
  226. {
  227. /* Need to always render: method, action and id
  228. */
  229. /* The name attribute is rendered _only_ if we're not in
  230. 2.0 mode or if the xhtml conformance mode is set to
  231. Legacy for 2.0 according to http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.name.aspx
  232. */
  233. string action;
  234. #if NET_2_0
  235. string customAction = Attributes ["action"];
  236. #endif
  237. Page p = Page;
  238. HttpRequest req = p != null ? p.Request : null;
  239. if (req == null)
  240. throw new HttpException ("No current request, cannot continue rendering.");
  241. #if !TARGET_J2EE
  242. #if NET_2_0
  243. if (String.IsNullOrEmpty (customAction)) {
  244. #endif
  245. string file_path = req.ClientFilePath;
  246. string current_path = req.CurrentExecutionFilePath;
  247. if (file_path == current_path) {
  248. // Just the filename will do
  249. action = UrlUtils.GetFile (file_path);
  250. } else {
  251. // Fun. We need to make cookieless sessions work, so no
  252. // absolute paths here.
  253. bool cookieless;
  254. #if NET_2_0
  255. SessionStateSection sec = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
  256. cookieless = sec != null ? sec.Cookieless == HttpCookieMode.UseUri: false;
  257. #else
  258. SessionConfig sec = HttpContext.GetAppConfig ("system.web/sessionState") as SessionConfig;
  259. cookieless = sec != null ? sec.CookieLess : false;
  260. #endif
  261. string appVPath = HttpRuntime.AppDomainAppVirtualPath;
  262. int appVPathLen = appVPath.Length;
  263. if (appVPathLen > 1) {
  264. if (cookieless) {
  265. if (StrUtils.StartsWith (file_path, appVPath, true))
  266. file_path = file_path.Substring (appVPathLen);
  267. } else if (StrUtils.StartsWith (current_path, appVPath, true))
  268. current_path = current_path.Substring (appVPathLen);
  269. }
  270. if (cookieless) {
  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. } else
  275. action = current_path;
  276. }
  277. #if NET_2_0
  278. } else
  279. action = customAction;
  280. #endif
  281. action += req.QueryStringRaw;
  282. #else
  283. // Allow the page to transform action to a portlet action url
  284. if (String.IsNullOrEmpty (customAction)) {
  285. string queryString = req.QueryStringRaw;
  286. action = CreateActionUrl (VirtualPathUtility.ToAppRelative (req.CurrentExecutionFilePath) +
  287. (string.IsNullOrEmpty (queryString) ? string.Empty : "?" + queryString));
  288. }
  289. else
  290. action = customAction;
  291. #endif
  292. #if NET_2_0
  293. XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as
  294. XhtmlConformanceSection;
  295. if (xhtml != null && xhtml.Mode == XhtmlConformanceMode.Legacy)
  296. #endif
  297. w.WriteAttribute ("name", Name);
  298. w.WriteAttribute ("method", Method);
  299. #if NET_2_0
  300. if (String.IsNullOrEmpty (customAction))
  301. #endif
  302. w.WriteAttribute ("action", action, true);
  303. /*
  304. * This is a hack that guarantees the ID is set properly for HtmlControl to
  305. * render it later on. As ugly as it is, we use it here because of the way
  306. * the ID, ClientID and UniqueID properties work internally in our Control
  307. * code.
  308. *
  309. * Fixes bug #82596
  310. */
  311. if (ID == null) {
  312. #pragma warning disable 219
  313. string client = ClientID;
  314. #pragma warning restore 219
  315. }
  316. string submit = Page.GetSubmitStatements ();
  317. if (submit != null && submit != "") {
  318. Attributes.Remove ("onsubmit");
  319. w.WriteAttribute ("onsubmit", submit);
  320. }
  321. /* enctype and target should not be written if
  322. * they are empty
  323. */
  324. string enctype = Enctype;
  325. if (enctype != null && enctype != "") {
  326. w.WriteAttribute ("enctype", enctype);
  327. }
  328. string target = Target;
  329. if (target != null && target != "") {
  330. w.WriteAttribute ("target", target);
  331. }
  332. #if NET_2_0
  333. string defaultbutton = DefaultButton;
  334. if (!String.IsNullOrEmpty (defaultbutton)) {
  335. Control c = FindControl (defaultbutton);
  336. if (c == null || !(c is IButtonControl))
  337. throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
  338. ID));
  339. if (DetermineRenderUplevel ()) {
  340. w.WriteAttribute (
  341. "onkeypress",
  342. "javascript:return " + Page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + c.ClientID + "')");
  343. }
  344. }
  345. #endif
  346. /* Now remove them from the hash so the base
  347. * RenderAttributes can do all the rest
  348. */
  349. Attributes.Remove ("method");
  350. Attributes.Remove ("enctype");
  351. Attributes.Remove ("target");
  352. base.RenderAttributes (w);
  353. }
  354. #if NET_2_0
  355. protected internal
  356. #else
  357. protected
  358. #endif
  359. override void RenderChildren (HtmlTextWriter w)
  360. {
  361. if (!inited) {
  362. Page.RegisterViewStateHandler ();
  363. #if NET_2_0
  364. Page.RegisterForm (this);
  365. #endif
  366. }
  367. Page.OnFormRender (w, ClientID);
  368. base.RenderChildren (w);
  369. Page.OnFormPostRender (w, ClientID);
  370. }
  371. #if NET_2_0
  372. /* According to corcompare */
  373. [MonoTODO ("why override?")]
  374. public override void RenderControl (HtmlTextWriter w)
  375. {
  376. base.RenderControl (w);
  377. }
  378. #endif
  379. #if NET_2_0
  380. protected internal
  381. #else
  382. protected
  383. #endif
  384. override void Render (HtmlTextWriter w)
  385. {
  386. base.Render (w);
  387. }
  388. }
  389. }