HtmlForm.cs 9.2 KB

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