HtmlForm.cs 9.1 KB

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