HtmlForm.cs 9.9 KB

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