HtmlForm.cs 8.8 KB

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