HtmlForm.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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;
  52. }
  53. set {
  54. defaultbutton = (value == null ? "" : value);
  55. }
  56. }
  57. string defaultfocus = "";
  58. [DefaultValue ("")]
  59. public string DefaultFocus
  60. {
  61. get {
  62. return defaultfocus;
  63. }
  64. set {
  65. defaultfocus = (value == null ? "" : 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. 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. return UplevelHelper.IsUplevel (
  196. System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
  197. #endif
  198. }
  199. protected internal override void OnPreRender (EventArgs e)
  200. {
  201. base.OnPreRender(e);
  202. }
  203. #endif
  204. protected override void RenderAttributes (HtmlTextWriter w)
  205. {
  206. /* Need to always render: method, action and id
  207. */
  208. /* The name attribute is rendered _only_ if we're not in
  209. 2.0 mode or if the xhtml conformance mode is set to
  210. Legacy for 2.0 according to http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.name.aspx
  211. */
  212. string action;
  213. string file_path = Page.Request.FilePath;
  214. string current_path = Page.Request.CurrentExecutionFilePath;
  215. if (file_path == current_path) {
  216. // Just the filename will do
  217. action = UrlUtils.GetFile (file_path);
  218. } else {
  219. // Fun. We need to make cookieless sessions work, so no
  220. // absolute paths here.
  221. Uri current_uri = new Uri ("http://host" + current_path);
  222. Uri fp_uri = new Uri ("http://host" + file_path);
  223. action = fp_uri.MakeRelative (current_uri);
  224. }
  225. action += Page.Request.QueryStringRaw;
  226. #if TARGET_J2EE
  227. // Allow the page to transform action to a portlet action url
  228. if (Page.IsPortletRender) {
  229. string customAction = Attributes ["action"];
  230. if (customAction != null && customAction.Length > 0)
  231. action = customAction;
  232. else
  233. action = Page.RenderResponse.createActionURL (action);
  234. }
  235. #endif
  236. #if NET_2_0
  237. XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as
  238. XhtmlConformanceSection;
  239. if (xhtml != null && xhtml.Mode == XhtmlConformanceMode.Legacy)
  240. #endif
  241. w.WriteAttribute ("name", Name);
  242. w.WriteAttribute ("method", Method);
  243. w.WriteAttribute ("action", action);
  244. if (ID == null) {
  245. /* If ID != null then HtmlControl will
  246. * write the id attribute
  247. */
  248. w.WriteAttribute ("id", ClientID);
  249. Attributes.Remove ("id");
  250. }
  251. string submit = Page.GetSubmitStatements ();
  252. if (submit != null && submit != "") {
  253. Attributes.Remove ("onsubmit");
  254. w.WriteAttribute ("onsubmit", submit);
  255. }
  256. /* enctype and target should not be written if
  257. * they are empty
  258. */
  259. string enctype = Enctype;
  260. if (enctype != null && enctype != "") {
  261. w.WriteAttribute ("enctype", enctype);
  262. }
  263. string target = Target;
  264. if (target != null && target != "") {
  265. w.WriteAttribute ("target", target);
  266. }
  267. #if NET_2_0
  268. string defaultbutton = DefaultButton;
  269. if (defaultbutton != null && defaultbutton != "") {
  270. Control c = FindControl (defaultbutton);
  271. if (c == null || !(c is IButtonControl))
  272. throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
  273. ID));
  274. }
  275. #endif
  276. /* Now remove them from the hash so the base
  277. * RenderAttributes can do all the rest
  278. */
  279. Attributes.Remove ("method");
  280. Attributes.Remove ("enctype");
  281. Attributes.Remove ("target");
  282. base.RenderAttributes (w);
  283. }
  284. #if NET_2_0
  285. protected internal
  286. #else
  287. protected
  288. #endif
  289. override void RenderChildren (HtmlTextWriter w)
  290. {
  291. if (!inited) {
  292. Page.RegisterViewStateHandler ();
  293. #if NET_2_0
  294. Page.RegisterForm (this);
  295. #endif
  296. }
  297. Page.OnFormRender (w, ClientID);
  298. base.RenderChildren (w);
  299. Page.OnFormPostRender (w, ClientID);
  300. }
  301. #if NET_2_0
  302. /* According to corcompare */
  303. [MonoTODO ("why override?")]
  304. public override void RenderControl (HtmlTextWriter w)
  305. {
  306. base.RenderControl (w);
  307. }
  308. #endif
  309. #if NET_2_0
  310. protected internal
  311. #else
  312. protected
  313. #endif
  314. override void Render (HtmlTextWriter w)
  315. {
  316. base.Render (w);
  317. }
  318. }
  319. }