HtmlForm.cs 8.5 KB

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