2 // System.Web.UI.HtmlControls.HtmlForm.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (C) 2005-2010 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;
35 using System.Web.SessionState;
37 namespace System.Web.UI.HtmlControls
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public class HtmlForm : HtmlContainerControl
46 string _defaultbutton;
47 bool submitdisabledcontrols = false;
50 public HtmlForm () : base ("form")
54 // LAMESPEC: This is undocumented on MSDN, but apparently it does exist on MS.NET.
55 // See https://bugzilla.novell.com/show_bug.cgi?id=442104
56 public string Action {
58 string action = Attributes ["action"];
59 if (String.IsNullOrEmpty (action))
66 if (String.IsNullOrEmpty (value))
67 Attributes ["action"] = null;
69 Attributes ["action"] = value;
74 public string DefaultButton {
76 return _defaultbutton ?? String.Empty;
79 _defaultbutton = value;
84 public string DefaultFocus {
86 return _defaultfocus ?? String.Empty;
89 _defaultfocus = value;
94 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
95 public string Enctype {
97 string enc = Attributes["enctype"];
100 return (String.Empty);
107 Attributes.Remove ("enctype");
109 Attributes["enctype"] = value;
115 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116 public string Method {
118 string method = Attributes["method"];
120 if ((method == null) || (method.Length == 0)) {
128 Attributes.Remove ("method");
130 Attributes["method"] = value;
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137 public virtual string Name {
142 /* why am i here? I do nothing. */
146 [DefaultValue (false)]
147 public virtual bool SubmitDisabledControls {
149 return submitdisabledcontrols;
152 submitdisabledcontrols = value;
157 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158 public string Target {
160 string target = Attributes["target"];
162 if (target == null) {
163 return (String.Empty);
170 Attributes.Remove ("target");
172 Attributes["target"] = value;
177 public override string UniqueID {
179 Control container = NamingContainer;
180 if (container == Page)
186 [MonoTODO ("why override?")]
187 protected override ControlCollection CreateControlCollection ()
189 return base.CreateControlCollection ();
192 protected internal override void OnInit (EventArgs e)
197 page.RegisterViewStateHandler ();
198 page.RegisterForm (this);
204 internal bool DetermineRenderUplevel ()
206 if (isUplevel != null)
207 return (bool) isUplevel;
209 isUplevel = UplevelHelper.IsUplevel (
210 System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
211 return (bool) isUplevel;
214 protected internal override void OnPreRender (EventArgs e)
219 protected override void RenderAttributes (HtmlTextWriter w)
221 /* Need to always render: method, action and id
225 string customAction = Attributes ["action"];
227 HttpRequest req = page != null ? page.RequestInternal : null;
228 if (String.IsNullOrEmpty (customAction)) {
229 string file_path = req != null ? req.ClientFilePath : null;
230 string current_path = req != null ? req.CurrentExecutionFilePath : null;
232 if (file_path == null)
234 else if (file_path == current_path) {
235 // Just the filename will do
236 action = UrlUtils.GetFile (file_path);
238 // Fun. We need to make cookieless sessions work, so no
239 // absolute paths here.
241 SessionStateSection sec = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
242 cookieless = sec != null ? sec.Cookieless == HttpCookieMode.UseUri: false;
243 string appVPath = HttpRuntime.AppDomainAppVirtualPath;
244 int appVPathLen = appVPath.Length;
246 if (appVPathLen > 1) {
248 if (StrUtils.StartsWith (file_path, appVPath, true))
249 file_path = file_path.Substring (appVPathLen + 1);
250 } else if (StrUtils.StartsWith (current_path, appVPath, true))
251 current_path = current_path.Substring (appVPathLen + 1);
255 Uri current_uri = new Uri ("http://host" + current_path);
256 Uri fp_uri = new Uri ("http://host" + file_path);
257 action = fp_uri.MakeRelative (current_uri);
259 action = current_path;
262 action = customAction;
264 action += req.QueryStringRaw;
266 XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as XhtmlConformanceSection;
267 if (xhtml == null || xhtml.Mode != XhtmlConformanceMode.Strict)
268 if (RenderingCompatibilityLessThan40)
269 // LAMESPEC: MSDN says the 'name' attribute is rendered only in
270 // Legacy mode, this is not true.
271 w.WriteAttribute ("name", Name);
274 w.WriteAttribute ("method", Method);
275 if (String.IsNullOrEmpty (customAction))
276 w.WriteAttribute ("action", action, true);
279 * This is a hack that guarantees the ID is set properly for HtmlControl to
280 * render it later on. As ugly as it is, we use it here because of the way
281 * the ID, ClientID and UniqueID properties work internally in our Control
287 #pragma warning disable 219
288 string client = ClientID;
289 #pragma warning restore 219
292 string submit = page != null ? page.GetSubmitStatements () : null;
293 if (!String.IsNullOrEmpty (submit)) {
294 Attributes.Remove ("onsubmit");
295 w.WriteAttribute ("onsubmit", submit);
298 /* enctype and target should not be written if
301 string enctype = Enctype;
302 if (!String.IsNullOrEmpty (enctype))
303 w.WriteAttribute ("enctype", enctype);
305 string target = Target;
306 if (!String.IsNullOrEmpty (target))
307 w.WriteAttribute ("target", target);
309 string defaultbutton = DefaultButton;
310 if (!String.IsNullOrEmpty (defaultbutton)) {
311 Control c = FindControl (defaultbutton);
313 if (c == null || !(c is IButtonControl))
314 throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
317 if (page != null && DetermineRenderUplevel ()) {
320 "javascript:return " + page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + c.ClientID + "')");
324 /* Now remove them from the hash so the base
325 * RenderAttributes can do all the rest
327 Attributes.Remove ("method");
328 Attributes.Remove ("enctype");
329 Attributes.Remove ("target");
331 base.RenderAttributes (w);
334 protected internal override void RenderChildren (HtmlTextWriter w)
338 if (!inited && page != null) {
339 page.RegisterViewStateHandler ();
340 page.RegisterForm (this);
343 page.OnFormRender (w, ClientID);
344 base.RenderChildren (w);
346 page.OnFormPostRender (w, ClientID);
349 /* According to corcompare */
350 [MonoTODO ("why override?")]
351 public override void RenderControl (HtmlTextWriter w)
353 base.RenderControl (w);
356 protected internal override void Render (HtmlTextWriter w)