HtmlForm.cs 11 KB

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