HtmlForm.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. public virtual bool SubmitDisabledControls
  131. {
  132. get {
  133. return submitdisabledcontrols;
  134. }
  135. set {
  136. submitdisabledcontrols = value;
  137. }
  138. }
  139. #endif
  140. [DefaultValue ("")]
  141. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  142. public string Target
  143. {
  144. get {
  145. string target = Attributes["target"];
  146. if (target == null) {
  147. return (String.Empty);
  148. }
  149. return (target);
  150. }
  151. set {
  152. if (value == null) {
  153. Attributes.Remove ("target");
  154. } else {
  155. Attributes["target"] = value;
  156. }
  157. }
  158. }
  159. #if NET_2_0
  160. public override
  161. #else
  162. // New in NET1.1 sp1
  163. public new
  164. #endif
  165. string UniqueID
  166. {
  167. get {
  168. return base.UniqueID;
  169. }
  170. }
  171. #if NET_2_0
  172. [MonoTODO ("why override?")]
  173. protected override ControlCollection CreateControlCollection ()
  174. {
  175. return base.CreateControlCollection ();
  176. }
  177. #endif
  178. #if NET_2_0
  179. protected internal
  180. #else
  181. protected
  182. #endif
  183. override void OnInit (EventArgs e)
  184. {
  185. Page.RegisterViewStateHandler ();
  186. #if NET_2_0
  187. Page.RegisterForm (this);
  188. #endif
  189. base.OnInit (e);
  190. }
  191. #if NET_2_0
  192. internal bool DetermineRenderUplevel ()
  193. {
  194. /* this bit is c&p'ed from BaseValidator.DetermineRenderUplevel */
  195. try {
  196. if (Page != null && Page.Request != null)
  197. return (
  198. /* From someplace on the web: "JavaScript 1.2
  199. * and later (also known as ECMAScript) has
  200. * built-in support for regular
  201. * expressions" */
  202. ((Page.Request.Browser.EcmaScriptVersion.Major == 1
  203. && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
  204. || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
  205. /* document.getElementById, .getAttribute,
  206. * etc, are all DOM level 1. I don't think we
  207. * use anything in level 2.. */
  208. && Page.Request.Browser.W3CDomVersion.Major >= 1);
  209. }
  210. catch {
  211. /* this can happen with a fake Page in nunit
  212. * tests, since Page.Context == null */
  213. ;
  214. }
  215. return false;
  216. }
  217. protected internal override void OnPreRender (EventArgs e)
  218. {
  219. string focus_id = null;
  220. bool need_script_block = false;
  221. bool render_uplevel;
  222. base.OnPreRender(e);
  223. render_uplevel = DetermineRenderUplevel ();
  224. /* figure out if we have some control we're going to focus */
  225. if (DefaultFocus != null && DefaultFocus != "")
  226. focus_id = DefaultFocus;
  227. else if (DefaultButton != null && DefaultButton != "")
  228. focus_id = DefaultButton;
  229. /* decide if we need to include the script block */
  230. need_script_block = (focus_id != null || submitdisabledcontrols);
  231. if (render_uplevel) {
  232. Page.RequiresPostBackScript();
  233. if (need_script_block && !Page.ClientScript.IsClientScriptBlockRegistered ("Mono-System.Web-HtmlScriptBlock")) {
  234. Page.ClientScript.RegisterClientScriptBlock ("Mono-System.Web-HtmlScriptBlock",
  235. String.Format ("<script language=\"JavaScript\" src=\"{0}\"></script>",
  236. Page.ClientScript.GetWebResourceUrl (GetType(),
  237. "webform.js")));
  238. }
  239. if (focus_id != null) {
  240. Page.ClientScript.RegisterStartupScript ("HtmlForm-DefaultButton-StartupScript",
  241. String.Format ("<script language=\"JavaScript\">\n" +
  242. "<!--\n" +
  243. "WebForm_AutoFocus('{0}');// -->\n" +
  244. "</script>\n", focus_id));
  245. }
  246. if (submitdisabledcontrols) {
  247. Page.ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
  248. "javascript: return WebForm_OnSubmit();");
  249. Page.ClientScript.RegisterStartupScript ("HtmlForm-SubmitDisabledControls-StartupScript",
  250. @"<script language=""JavaScript"">
  251. <!--
  252. function WebForm_OnSubmit() {
  253. WebForm_ReEnableControls();
  254. return true;
  255. } // -->
  256. </script>");
  257. }
  258. }
  259. }
  260. #endif
  261. protected override void RenderAttributes (HtmlTextWriter w)
  262. {
  263. /* Need to always render: name, method, action
  264. * and id
  265. */
  266. string action = Page.Request.FilePath;
  267. string query = Page.Request.QueryStringRaw;
  268. if (query != null && query.Length > 0) {
  269. action += "?" + query;
  270. }
  271. w.WriteAttribute ("name", Name);
  272. w.WriteAttribute ("method", Method);
  273. w.WriteAttribute ("action", action);
  274. if (ID == null) {
  275. /* If ID != null then HtmlControl will
  276. * write the id attribute
  277. */
  278. w.WriteAttribute ("id", ClientID);
  279. Attributes.Remove ("id");
  280. }
  281. string submit = Page.GetSubmitStatements ();
  282. if (submit != null && submit != "")
  283. w.WriteAttribute ("onsubmit", submit);
  284. /* enctype and target should not be written if
  285. * they are empty
  286. */
  287. string enctype = Enctype;
  288. if (enctype != null && enctype != "") {
  289. w.WriteAttribute ("enctype", enctype);
  290. }
  291. string target = Target;
  292. if (target != null && target != "") {
  293. w.WriteAttribute ("target", target);
  294. }
  295. #if NET_2_0
  296. string defaultbutton = DefaultButton;
  297. if (defaultbutton != null && defaultbutton != "") {
  298. Control c = FindControl (defaultbutton);
  299. if (c == null || !(c is IButtonControl))
  300. throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
  301. ID));
  302. }
  303. #endif
  304. /* Now remove them from the hash so the base
  305. * RenderAttributes can do all the rest
  306. */
  307. Attributes.Remove ("name");
  308. Attributes.Remove ("method");
  309. Attributes.Remove ("enctype");
  310. Attributes.Remove ("target");
  311. base.RenderAttributes (w);
  312. }
  313. #if NET_2_0
  314. protected internal
  315. #else
  316. protected
  317. #endif
  318. override void RenderChildren (HtmlTextWriter w)
  319. {
  320. Page.OnFormRender (w, ClientID);
  321. base.RenderChildren (w);
  322. Page.OnFormPostRender (w, ClientID);
  323. }
  324. #if NET_2_0
  325. /* According to corcompare */
  326. [MonoTODO ("why override?")]
  327. public override void RenderControl (HtmlTextWriter w)
  328. {
  329. base.RenderControl (w);
  330. }
  331. #endif
  332. #if NET_2_0
  333. protected internal
  334. #else
  335. protected
  336. #endif
  337. override void Render (HtmlTextWriter w)
  338. {
  339. base.Render (w);
  340. }
  341. }
  342. }