2 // System.Web.UI.HtmlControls.HtmlForm.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
32 using System.Web.Util;
33 using System.Web.UI.WebControls;
34 using System.Web.Configuration;
36 namespace System.Web.UI.HtmlControls
39 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public class HtmlForm : HtmlContainerControl
45 public HtmlForm () : base ("form")
50 string _defaultbutton;
52 public string DefaultButton
55 return _defaultbutton ?? String.Empty;
58 _defaultbutton = value;
64 public string DefaultFocus
67 return _defaultfocus ?? String.Empty;
70 _defaultfocus = value;
76 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80 string enc = Attributes["enctype"];
83 return (String.Empty);
90 Attributes.Remove ("enctype");
92 Attributes["enctype"] = value;
98 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102 string method = Attributes["method"];
104 if ((method == null) || (method.Length == 0)) {
112 Attributes.Remove ("method");
114 Attributes["method"] = value;
120 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121 public virtual string Name
127 /* why am i here? I do nothing. */
132 bool submitdisabledcontrols = false;
133 [DefaultValue (false)]
134 public virtual bool SubmitDisabledControls
137 return submitdisabledcontrols;
140 submitdisabledcontrols = value;
146 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150 string target = Attributes["target"];
152 if (target == null) {
153 return (String.Empty);
160 Attributes.Remove ("target");
162 Attributes["target"] = value;
167 public override string UniqueID {
169 return base.UniqueID;
174 [MonoTODO ("why override?")]
175 protected override ControlCollection CreateControlCollection ()
177 return base.CreateControlCollection ();
186 override void OnInit (EventArgs e)
189 Page.RegisterViewStateHandler ();
192 Page.RegisterForm (this);
200 internal bool DetermineRenderUplevel ()
203 if (HttpContext.Current == null)
207 /* From someplace on the web: "JavaScript 1.2
208 * and later (also known as ECMAScript) has
209 * built-in support for regular
211 ((Page.Request.Browser.EcmaScriptVersion.Major == 1
212 && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
213 || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
215 /* document.getElementById, .getAttribute,
216 * etc, are all DOM level 1. I don't think we
217 * use anything in level 2.. */
218 && Page.Request.Browser.W3CDomVersion.Major >= 1);
220 if (isUplevel != null)
221 return (bool) isUplevel;
223 isUplevel = UplevelHelper.IsUplevel (
224 System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
225 return (bool) isUplevel;
229 protected internal override void OnPreRender (EventArgs e)
235 protected override void RenderAttributes (HtmlTextWriter w)
237 /* Need to always render: method, action and id
239 /* The name attribute is rendered _only_ if we're not in
240 2.0 mode or if the xhtml conformance mode is set to
241 Legacy for 2.0 according to http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.name.aspx
246 string file_path = Page.Request.FilePath;
247 string current_path = Page.Request.CurrentExecutionFilePath;
248 if (file_path == current_path) {
249 // Just the filename will do
250 action = UrlUtils.GetFile (file_path);
252 // Fun. We need to make cookieless sessions work, so no
253 // absolute paths here.
254 Uri current_uri = new Uri ("http://host" + current_path);
255 Uri fp_uri = new Uri ("http://host" + file_path);
256 action = fp_uri.MakeRelative (current_uri);
259 action += Page.Request.QueryStringRaw;
261 // Allow the page to transform action to a portlet action url
262 string customAction = Attributes ["action"];
263 if (String.IsNullOrEmpty (customAction)) {
264 string queryString = Page.Request.QueryStringRaw;
265 action = CreateActionUrl (VirtualPathUtility.ToAppRelative (Page.Request.CurrentExecutionFilePath) +
266 (string.IsNullOrEmpty (queryString) ? string.Empty : "?" + queryString));
269 action = customAction;
274 XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as
275 XhtmlConformanceSection;
277 if (xhtml != null && xhtml.Mode == XhtmlConformanceMode.Legacy)
279 w.WriteAttribute ("name", Name);
281 w.WriteAttribute ("method", Method);
282 w.WriteAttribute ("action", action, true);
285 * This is a hack that guarantees the ID is set properly for HtmlControl to
286 * render it later on. As ugly as it is, we use it here because of the way
287 * the ID, ClientID and UniqueID properties work internally in our Control
293 #pragma warning disable 219
294 string client = ClientID;
295 #pragma warning restore 219
298 string submit = Page.GetSubmitStatements ();
299 if (submit != null && submit != "") {
300 Attributes.Remove ("onsubmit");
301 w.WriteAttribute ("onsubmit", submit);
304 /* enctype and target should not be written if
307 string enctype = Enctype;
308 if (enctype != null && enctype != "") {
309 w.WriteAttribute ("enctype", enctype);
312 string target = Target;
313 if (target != null && target != "") {
314 w.WriteAttribute ("target", target);
318 string defaultbutton = DefaultButton;
319 if (!String.IsNullOrEmpty (defaultbutton)) {
320 Control c = FindControl (defaultbutton);
322 if (c == null || !(c is IButtonControl))
323 throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
326 if (DetermineRenderUplevel ()) {
329 "javascript:return " + Page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + c.ClientID + "')");
334 /* Now remove them from the hash so the base
335 * RenderAttributes can do all the rest
337 Attributes.Remove ("method");
338 Attributes.Remove ("enctype");
339 Attributes.Remove ("target");
341 base.RenderAttributes (w);
349 override void RenderChildren (HtmlTextWriter w)
352 Page.RegisterViewStateHandler ();
354 Page.RegisterForm (this);
357 Page.OnFormRender (w, ClientID);
358 base.RenderChildren (w);
359 Page.OnFormPostRender (w, ClientID);
363 /* According to corcompare */
364 [MonoTODO ("why override?")]
365 public override void RenderControl (HtmlTextWriter w)
367 base.RenderControl (w);
376 override void Render (HtmlTextWriter w)