Add portal support
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlForm.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlForm.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
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
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
35 namespace System.Web.UI.HtmlControls 
36 {
37         // CAS
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public class HtmlForm : HtmlContainerControl 
41         {
42                 bool inited;
43
44                 public HtmlForm () : base ("form")
45                 {
46                 }
47
48 #if NET_2_0
49                 string defaultbutton = "";
50                 [DefaultValue ("")]
51                 public string DefaultButton
52                 {
53                         get {
54                                 return defaultbutton;
55                         }
56                         set {
57                                 defaultbutton = (value == null ? "" : value);
58                         }
59                 }
60
61                 string defaultfocus = "";
62                 [DefaultValue ("")]
63                 public string DefaultFocus
64                 {
65                         get {
66                                 return defaultfocus;
67                         }
68                         set {
69                                 defaultfocus = (value == null ? "" : value);
70                         }
71                 }
72 #endif          
73
74                 [DefaultValue ("")]
75                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
76                 public string Enctype 
77                 {
78                         get {
79                                 string enc = Attributes["enctype"];
80
81                                 if (enc == null) {
82                                         return (String.Empty);
83                                 }
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                 
96                 [DefaultValue ("")]
97                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
98                 public string Method 
99                 {
100                         get {
101                                 string method = Attributes["method"];
102
103                                 if ((method == null) || (method.Length == 0)) {
104                                         return ("post");
105                                 }
106                                 
107                                 return (method);
108                         }
109                         set {
110                                 if (value == null) {
111                                         Attributes.Remove ("method");
112                                 } else {
113                                         Attributes["method"] = value;
114                                 }
115                         }
116                 }
117
118                 [DefaultValue ("")]
119                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
120                 public virtual string Name 
121                 {
122                         get {
123                                 return UniqueID;
124                         }
125                         set {
126                                 /* why am i here? I do nothing. */
127                         }
128                 }
129
130 #if NET_2_0
131                 bool submitdisabledcontrols = false;
132                 [DefaultValue (false)]
133                 public virtual bool SubmitDisabledControls 
134                 {
135                         get {
136                                 return submitdisabledcontrols;
137                         }
138                         set {
139                                 submitdisabledcontrols = value;
140                         }
141                 }
142 #endif
143                         
144                 [DefaultValue ("")]
145                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
146                 public string Target 
147                 {
148                         get {
149                                 string target = Attributes["target"];
150
151                                 if (target == null) {
152                                         return (String.Empty);
153                                 }
154                                 
155                                 return (target);
156                         }
157                         set {
158                                 if (value == null) {
159                                         Attributes.Remove ("target");
160                                 } else {
161                                         Attributes["target"] = value;
162                                 }
163                         }
164                 }
165
166                 public override string UniqueID {
167                         get {
168                                 return base.UniqueID;
169                         }
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
180 #if NET_2_0
181                 protected internal
182 #else           
183                 protected
184 #endif          
185                 override void OnInit (EventArgs e)
186                 {
187                         inited = true;
188                         Page.RegisterViewStateHandler ();
189
190 #if NET_2_0
191                         Page.RegisterForm (this);
192 #endif
193
194                         base.OnInit (e);
195                 }
196
197 #if NET_2_0
198                 internal bool DetermineRenderUplevel ()
199                 {
200                         /* this bit is c&p'ed from BaseValidator.DetermineRenderUplevel */
201                         try {
202                                 if (Page != null && Page.Request != null)
203                                         return (
204                                                 /* From someplace on the web: "JavaScript 1.2
205                                                  * and later (also known as ECMAScript) has
206                                                  * built-in support for regular
207                                                  * expressions" */
208                                                 ((Page.Request.Browser.EcmaScriptVersion.Major == 1
209                                                   && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
210                                                  || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
211
212                                                 /* document.getElementById, .getAttribute,
213                                                  * etc, are all DOM level 1.  I don't think we
214                                                  * use anything in level 2.. */
215                                                 && Page.Request.Browser.W3CDomVersion.Major >= 1);
216                         }
217                         catch {
218                                 /* this can happen with a fake Page in nunit
219                                  * tests, since Page.Context == null */
220                                 ;
221                         }
222
223                         return false;
224                 }
225
226                 protected internal override void OnPreRender (EventArgs e)
227                 {
228                         base.OnPreRender(e);
229                 }
230 #endif          
231
232                 protected override void RenderAttributes (HtmlTextWriter w)
233                 {
234                         /* Need to always render: name, method, action
235                          * and id
236                          */
237
238                         string action;
239                         string file_path = Page.Request.FilePath;
240                         string current_path = Page.Request.CurrentExecutionFilePath;
241                         if (file_path == current_path) {
242                                 // Just the filename will do
243                                 action = UrlUtils.GetFile (file_path);
244                         } else {
245                                 // Fun. We need to make cookieless sessions work, so no
246                                 // absolute paths here.
247                                 Uri current_uri = new Uri ("http://host" + current_path);
248                                 Uri fp_uri = new Uri ("http://host" + file_path);
249                                 action = fp_uri.MakeRelative (current_uri);
250                         }
251
252                         action += Page.Request.QueryStringRaw;
253 #if TARGET_J2EE
254                         vmw.@internal.j2ee.IPortletRenderResponse resp = GetRenderResponse();
255                         if (resp != null)
256                                 action = resp.createActionURL(action);
257 #endif
258
259                         w.WriteAttribute ("name", Name);
260
261                         w.WriteAttribute ("method", Method);
262                         w.WriteAttribute ("action", action);
263
264                         if (ID == null) {
265                                 /* If ID != null then HtmlControl will
266                                  * write the id attribute
267                                  */
268                                 w.WriteAttribute ("id", ClientID);
269                                 Attributes.Remove ("id");
270                         }
271
272                         string submit = Page.GetSubmitStatements ();
273                         if (submit != null && submit != "") {
274                                 Attributes.Remove ("onsubmit");
275                                 w.WriteAttribute ("onsubmit", submit);
276                         }
277                         
278                         /* enctype and target should not be written if
279                          * they are empty
280                          */
281                         string enctype = Enctype;
282                         if (enctype != null && enctype != "") {
283                                 w.WriteAttribute ("enctype", enctype);
284                         }
285
286                         string target = Target;
287                         if (target != null && target != "") {
288                                 w.WriteAttribute ("target", target);
289                         }
290
291 #if NET_2_0
292                         string defaultbutton = DefaultButton;
293                         if (defaultbutton != null && defaultbutton != "") {
294                                 Control c = FindControl (defaultbutton);
295
296                                 if (c == null || !(c is IButtonControl))
297                                         throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
298                                                                                            ID));
299                         }
300 #endif
301
302                         /* Now remove them from the hash so the base
303                          * RenderAttributes can do all the rest
304                          */
305                         Attributes.Remove ("method");
306                         Attributes.Remove ("enctype");
307                         Attributes.Remove ("target");
308
309                         base.RenderAttributes (w);
310                 }
311
312 #if NET_2_0
313                 protected internal
314 #else           
315                 protected
316 #endif          
317                 override void RenderChildren (HtmlTextWriter w)
318                 {
319                         if (!inited) {
320                                 Page.RegisterViewStateHandler ();
321 #if NET_2_0
322                                 Page.RegisterForm (this);
323 #endif
324                         }
325                         Page.OnFormRender (w, ClientID);
326                         base.RenderChildren (w);
327                         Page.OnFormPostRender (w, ClientID);
328                 }
329
330 #if NET_2_0
331                 /* According to corcompare */
332                 [MonoTODO ("why override?")]
333                 public override void RenderControl (HtmlTextWriter w)
334                 {
335                         base.RenderControl (w);
336                 }
337 #endif          
338
339 #if NET_2_0
340                 protected internal
341 #else           
342                 protected
343 #endif          
344                 override void Render (HtmlTextWriter w)
345                 {
346                         base.Render (w);
347                 }
348         }
349 }
350
351