HtmlForm.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. namespace System.Web.UI.HtmlControls
  34. {
  35. // CAS
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. public class HtmlForm : HtmlContainerControl
  39. {
  40. bool inited;
  41. public HtmlForm () : base ("form")
  42. {
  43. }
  44. #if NET_2_0
  45. string defaultbutton = "";
  46. [DefaultValue ("")]
  47. public string DefaultButton
  48. {
  49. get {
  50. return defaultbutton;
  51. }
  52. set {
  53. defaultbutton = (value == null ? "" : value);
  54. }
  55. }
  56. string defaultfocus = "";
  57. [DefaultValue ("")]
  58. public string DefaultFocus
  59. {
  60. get {
  61. return defaultfocus;
  62. }
  63. set {
  64. defaultfocus = (value == null ? "" : value);
  65. }
  66. }
  67. #endif
  68. [DefaultValue ("")]
  69. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  70. public string Enctype
  71. {
  72. get {
  73. string enc = Attributes["enctype"];
  74. if (enc == null) {
  75. return (String.Empty);
  76. }
  77. return (enc);
  78. }
  79. set {
  80. if (value == null) {
  81. Attributes.Remove ("enctype");
  82. } else {
  83. Attributes["enctype"] = value;
  84. }
  85. }
  86. }
  87. [DefaultValue ("")]
  88. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  89. public string Method
  90. {
  91. get {
  92. string method = Attributes["method"];
  93. if ((method == null) || (method.Length == 0)) {
  94. return ("post");
  95. }
  96. return (method);
  97. }
  98. set {
  99. if (value == null) {
  100. Attributes.Remove ("method");
  101. } else {
  102. Attributes["method"] = value;
  103. }
  104. }
  105. }
  106. [DefaultValue ("")]
  107. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  108. public virtual string Name
  109. {
  110. get {
  111. return UniqueID;
  112. }
  113. set {
  114. /* why am i here? I do nothing. */
  115. }
  116. }
  117. #if NET_2_0
  118. bool submitdisabledcontrols = false;
  119. [DefaultValue (false)]
  120. public virtual bool SubmitDisabledControls
  121. {
  122. get {
  123. return submitdisabledcontrols;
  124. }
  125. set {
  126. submitdisabledcontrols = value;
  127. }
  128. }
  129. #endif
  130. [DefaultValue ("")]
  131. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  132. public string Target
  133. {
  134. get {
  135. string target = Attributes["target"];
  136. if (target == null) {
  137. return (String.Empty);
  138. }
  139. return (target);
  140. }
  141. set {
  142. if (value == null) {
  143. Attributes.Remove ("target");
  144. } else {
  145. Attributes["target"] = value;
  146. }
  147. }
  148. }
  149. public override string UniqueID {
  150. get {
  151. return base.UniqueID;
  152. }
  153. }
  154. #if NET_2_0
  155. [MonoTODO ("why override?")]
  156. protected override ControlCollection CreateControlCollection ()
  157. {
  158. return base.CreateControlCollection ();
  159. }
  160. #endif
  161. #if NET_2_0
  162. protected internal
  163. #else
  164. protected
  165. #endif
  166. override void OnInit (EventArgs e)
  167. {
  168. inited = true;
  169. Page.RegisterViewStateHandler ();
  170. #if NET_2_0
  171. Page.RegisterForm (this);
  172. #endif
  173. base.OnInit (e);
  174. }
  175. #if NET_2_0
  176. internal bool DetermineRenderUplevel ()
  177. {
  178. /* this bit is c&p'ed from BaseValidator.DetermineRenderUplevel */
  179. try {
  180. if (Page != null && Page.Request != null)
  181. return (
  182. /* From someplace on the web: "JavaScript 1.2
  183. * and later (also known as ECMAScript) has
  184. * built-in support for regular
  185. * expressions" */
  186. ((Page.Request.Browser.EcmaScriptVersion.Major == 1
  187. && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
  188. || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
  189. /* document.getElementById, .getAttribute,
  190. * etc, are all DOM level 1. I don't think we
  191. * use anything in level 2.. */
  192. && Page.Request.Browser.W3CDomVersion.Major >= 1);
  193. }
  194. catch {
  195. /* this can happen with a fake Page in nunit
  196. * tests, since Page.Context == null */
  197. ;
  198. }
  199. return false;
  200. }
  201. protected internal override void OnPreRender (EventArgs e)
  202. {
  203. base.OnPreRender(e);
  204. }
  205. #endif
  206. protected override void RenderAttributes (HtmlTextWriter w)
  207. {
  208. /* Need to always render: name, method, action
  209. * and id
  210. */
  211. string action;
  212. string file_path = Page.Request.FilePath;
  213. string current_path = Page.Request.CurrentExecutionFilePath;
  214. if (file_path == current_path) {
  215. // Just the filename will do
  216. action = UrlUtils.GetFile (file_path);
  217. } else {
  218. // Fun. We need to make cookieless sessions work, so no
  219. // absolute paths here.
  220. Uri current_uri = new Uri ("http://host" + current_path);
  221. Uri fp_uri = new Uri ("http://host" + file_path);
  222. action = fp_uri.MakeRelative (current_uri);
  223. }
  224. action += Page.Request.QueryStringRaw;
  225. w.WriteAttribute ("name", Name);
  226. w.WriteAttribute ("method", Method);
  227. w.WriteAttribute ("action", action);
  228. if (ID == null) {
  229. /* If ID != null then HtmlControl will
  230. * write the id attribute
  231. */
  232. w.WriteAttribute ("id", ClientID);
  233. Attributes.Remove ("id");
  234. }
  235. string submit = Page.GetSubmitStatements ();
  236. if (submit != null && submit != "")
  237. w.WriteAttribute ("onsubmit", submit);
  238. /* enctype and target should not be written if
  239. * they are empty
  240. */
  241. string enctype = Enctype;
  242. if (enctype != null && enctype != "") {
  243. w.WriteAttribute ("enctype", enctype);
  244. }
  245. string target = Target;
  246. if (target != null && target != "") {
  247. w.WriteAttribute ("target", target);
  248. }
  249. #if NET_2_0
  250. string defaultbutton = DefaultButton;
  251. if (defaultbutton != null && defaultbutton != "") {
  252. Control c = FindControl (defaultbutton);
  253. if (c == null || !(c is IButtonControl))
  254. throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
  255. ID));
  256. }
  257. #endif
  258. /* Now remove them from the hash so the base
  259. * RenderAttributes can do all the rest
  260. */
  261. Attributes.Remove ("method");
  262. Attributes.Remove ("enctype");
  263. Attributes.Remove ("target");
  264. base.RenderAttributes (w);
  265. }
  266. #if NET_2_0
  267. protected internal
  268. #else
  269. protected
  270. #endif
  271. override void RenderChildren (HtmlTextWriter w)
  272. {
  273. if (!inited) {
  274. Page.RegisterViewStateHandler ();
  275. #if NET_2_0
  276. Page.RegisterForm (this);
  277. #endif
  278. }
  279. Page.OnFormRender (w, ClientID);
  280. base.RenderChildren (w);
  281. Page.OnFormPostRender (w, ClientID);
  282. }
  283. #if NET_2_0
  284. /* According to corcompare */
  285. [MonoTODO ("why override?")]
  286. public override void RenderControl (HtmlTextWriter w)
  287. {
  288. base.RenderControl (w);
  289. }
  290. #endif
  291. #if NET_2_0
  292. protected internal
  293. #else
  294. protected
  295. #endif
  296. override void Render (HtmlTextWriter w)
  297. {
  298. base.Render (w);
  299. }
  300. }
  301. }