New test.
[mono.git] / mcs / class / System.Web / System.Web.UI / Page.cs
1 //
2 // System.Web.UI.Page.cs
3 //
4 // Authors:
5 //   Duncan Mak  (duncan@ximian.com)
6 //   Gonzalo Paniagua (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2003,2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
37 using System.ComponentModel.Design.Serialization;
38 using System.Globalization;
39 using System.IO;
40 using System.Security.Cryptography;
41 using System.Security.Permissions;
42 using System.Security.Principal;
43 using System.Text;
44 using System.Threading;
45 using System.Web;
46 using System.Web.Caching;
47 using System.Web.Configuration;
48 using System.Web.SessionState;
49 using System.Web.Util;
50 using System.Web.UI.HtmlControls;
51 using System.Web.UI.WebControls;
52 #if NET_2_0
53 using System.Web.UI.Adapters;
54 #endif
55
56 namespace System.Web.UI
57 {
58 // CAS
59 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
60 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
61 #if !NET_2_0
62 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
63 #endif
64 [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
65 [ToolboxItem (false)]
66 #if NET_2_0
67 [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
68 #else
69 [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
70 #endif
71 public class Page : TemplateControl, IHttpHandler
72 {
73 #if NET_2_0
74         private bool _eventValidation = true;
75 #endif
76         private bool _viewState = true;
77         private bool _viewStateMac;
78         private string _errorPage;
79         private bool is_validated;
80         private bool _smartNavigation;
81         private int _transactionMode;
82         private HttpContext _context;
83         private ValidatorCollection _validators;
84         private bool renderingForm;
85         private object _savedViewState;
86         private ArrayList _requiresPostBack;
87         private ArrayList _requiresPostBackCopy;
88         private ArrayList requiresPostDataChanged;
89         private IPostBackEventHandler requiresRaiseEvent;
90         private NameValueCollection secondPostData;
91         private bool requiresPostBackScript;
92         private bool postBackScriptRendered;
93         bool handleViewState;
94         string viewStateUserKey;
95         NameValueCollection _requestValueCollection;
96         string clientTarget;
97         ClientScriptManager scriptManager;
98         bool allow_load; // true when the Form collection belongs to this page (GetTypeHashCode)
99
100         [EditorBrowsable (EditorBrowsableState.Never)]
101 #if NET_2_0
102         public
103 #else
104         protected
105 #endif
106         const string postEventArgumentID = "__EVENTARGUMENT";
107
108         [EditorBrowsable (EditorBrowsableState.Never)]
109 #if NET_2_0
110         public
111 #else
112         protected
113 #endif
114         const string postEventSourceID = "__EVENTTARGET";
115
116 #if NET_2_0
117         const string ScrollPositionXID = "__SCROLLPOSITIONX";
118         const string ScrollPositionYID = "__SCROLLPOSITIONY";
119 #endif
120
121 #if NET_2_0
122         internal const string CallbackArgumentID = "__CALLBACKARGUMENT";
123         internal const string CallbackSourceID = "__CALLBACKTARGET";
124         internal const string PreviousPageID = "__PREVIOUSPAGE";
125         
126         HtmlHead htmlHeader;
127         
128         MasterPage masterPage;
129         string masterPageFile;
130         
131         Page previousPage;
132         bool isCrossPagePostBack;
133         bool isPostBack;
134         bool isCallback;
135         ArrayList requireStateControls;
136         Hashtable _validatorsByGroup;
137         HtmlForm _form;
138
139         string _title;
140         string _theme;
141         string _styleSheetTheme;
142         Hashtable items;
143
144         bool _maintainScrollPositionOnPostBack;
145 #endif
146
147         #region Constructor
148         public Page ()
149         {
150                 scriptManager = new ClientScriptManager (this);
151                 Page = this;
152                 ID = "__Page";
153         }
154
155         #endregion              
156
157         #region Properties
158
159         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
160         [Browsable (false)]
161         public HttpApplicationState Application
162         {
163                 get {
164                         if (_context == null)
165                                 return null;
166                         return _context.Application;
167                 }
168         }
169
170         [EditorBrowsable (EditorBrowsableState.Never)]
171         protected bool AspCompatMode
172         {
173 #if NET_2_0
174                 get { return false; }
175 #endif
176                 set { throw new NotImplementedException (); }
177         }
178
179         [EditorBrowsable (EditorBrowsableState.Never)]
180 #if NET_2_0
181         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
182         [BrowsableAttribute (false)]
183         public bool Buffer
184         {
185                 get { return Response.BufferOutput; }
186                 set { Response.BufferOutput = value; }
187         }
188 #else
189         protected bool Buffer
190         {
191                 set { Response.BufferOutput = value; }
192         }
193 #endif
194
195         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
196         [Browsable (false)]
197         public Cache Cache
198         {
199                 get {
200                         if (_context == null)
201                                 throw new HttpException ("No cache available without a context.");
202                         return _context.Cache;
203                 }
204         }
205
206 #if NET_2_0
207         [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
208 #endif
209         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
210         [Browsable (false), DefaultValue ("")]
211         [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
212         public string ClientTarget
213         {
214                 get { return (clientTarget == null) ? "" : clientTarget; }
215                 set {
216                         clientTarget = value;
217                         if (value == "")
218                                 clientTarget = null;
219                 }
220         }
221
222         [EditorBrowsable (EditorBrowsableState.Never)]
223 #if NET_2_0
224         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
225         [BrowsableAttribute (false)]
226         public int CodePage
227         {
228                 get { return Response.ContentEncoding.CodePage; }
229                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
230         }
231 #else
232         protected int CodePage
233         {
234                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
235         }
236 #endif
237
238         [EditorBrowsable (EditorBrowsableState.Never)]
239 #if NET_2_0
240         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
241         [BrowsableAttribute (false)]
242         public string ContentType
243         {
244                 get { return Response.ContentType; }
245                 set { Response.ContentType = value; }
246         }
247 #else
248         protected string ContentType
249         {
250                 set { Response.ContentType = value; }
251         }
252 #endif
253
254         protected override HttpContext Context
255         {
256                 get {
257                         if (_context == null)
258                                 return HttpContext.Current;
259
260                         return _context;
261                 }
262         }
263
264 #if NET_2_0
265         [EditorBrowsable (EditorBrowsableState.Advanced)]
266         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
267         [BrowsableAttribute (false)]
268         public string Culture
269         {
270                 get { return Thread.CurrentThread.CurrentCulture.Name; }
271                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
272         }
273 #else
274         [EditorBrowsable (EditorBrowsableState.Never)]
275         protected string Culture
276         {
277                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
278         }
279 #endif
280
281 #if NET_2_0
282         public virtual bool EnableEventValidation {
283                 get { return _eventValidation; }
284                 set { _eventValidation = value;}
285         }
286 #endif
287
288         [Browsable (false)]
289         public override bool EnableViewState
290         {
291                 get { return _viewState; }
292                 set { _viewState = value; }
293         }
294
295 #if NET_2_0
296         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
297         [BrowsableAttribute (false)]
298 #endif
299         [EditorBrowsable (EditorBrowsableState.Never)]
300 #if NET_2_0
301         public
302 #else
303         protected
304 #endif
305         bool EnableViewStateMac
306         {
307                 get { return _viewStateMac; }
308                 set { _viewStateMac = value; }
309         }
310
311         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
312         [Browsable (false), DefaultValue ("")]
313         [WebSysDescription ("The URL of a page used for error redirection.")]
314         public string ErrorPage
315         {
316                 get { return _errorPage; }
317                 set {
318                         _errorPage = value;
319                         if (_context != null)
320                                 _context.ErrorPage = value;
321                 }
322         }
323
324 #if NET_2_0
325         [Obsolete]
326 #endif
327         [EditorBrowsable (EditorBrowsableState.Never)]
328         protected ArrayList FileDependencies
329         {
330                 set {
331                         if (Response != null)
332                                 Response.AddFileDependencies (value);
333                 }
334         }
335
336         [Browsable (false)]
337 #if NET_2_0
338         [EditorBrowsable (EditorBrowsableState.Never)]
339 #endif
340         public override string ID
341         {
342                 get { return base.ID; }
343                 set { base.ID = value; }
344         }
345
346         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
347         [Browsable (false)]
348         public bool IsPostBack
349         {
350                 get {
351 #if NET_2_0
352                         return isPostBack;
353 #else
354                         return _requestValueCollection != null;
355 #endif
356                 }
357         }
358
359         [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
360         public bool IsReusable {
361                 get { return false; }
362         }
363
364         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
365         [Browsable (false)]
366         public bool IsValid {
367                 get {
368                         if (!is_validated)
369                                 throw new HttpException (Locale.GetText ("Page hasn't been validated."));
370
371 #if NET_2_0
372                         foreach (IValidator val in Validators)
373                                 if (!val.IsValid)
374                                         return false;
375                         return true;
376 #else
377                         return ValidateCollection (_validators);
378 #endif
379                 }
380         }
381 #if NET_2_0
382         public IDictionary Items {
383                 get {
384                         if (items == null)
385                                 items = new Hashtable ();
386                         return items;
387                 }
388         }
389 #endif
390
391         [EditorBrowsable (EditorBrowsableState.Never)]
392 #if NET_2_0
393         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
394         [BrowsableAttribute (false)]
395         public int LCID {
396                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
397                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
398         }
399 #else
400         protected int LCID {
401                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
402         }
403 #endif
404
405 #if NET_2_0
406         [Browsable (false)]
407         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
408         public bool MaintainScrollPositionOnPostBack {
409                 get { return _maintainScrollPositionOnPostBack; }
410                 set { _maintainScrollPositionOnPostBack = value; }
411         }
412 #endif
413
414 #if NET_2_0
415         public PageAdapter PageAdapter {
416                 get {
417                         return (PageAdapter)Adapter;
418                 }
419         }
420 #endif
421
422         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
423         [Browsable (false)]
424         public HttpRequest Request
425         {
426                 get {
427                         if (_context != null)
428                                 return _context.Request;
429
430                         throw new HttpException("Request is not available without context");
431                 }
432         }
433
434         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
435         [Browsable (false)]
436         public HttpResponse Response
437         {
438                 get {
439                         if (_context != null)
440                                 return _context.Response;
441
442                         throw new HttpException ("Response is not available without context");
443                 }
444         }
445
446         [EditorBrowsable (EditorBrowsableState.Never)]
447 #if NET_2_0
448         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
449         [BrowsableAttribute (false)]
450         public string ResponseEncoding
451         {
452                 get { return Response.ContentEncoding.WebName; }
453                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
454         }
455 #else
456         protected string ResponseEncoding
457         {
458                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
459         }
460 #endif
461
462         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
463         [Browsable (false)]
464         public HttpServerUtility Server
465         {
466                 get {
467                         return Context.Server;
468                 }
469         }
470
471         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
472         [Browsable (false)]
473         public virtual HttpSessionState Session
474         {
475                 get {
476                         if (_context == null)
477                                 _context = HttpContext.Current;
478
479                         if (_context == null)
480                                 throw new HttpException ("Session is not available without context");
481
482                         if (_context.Session == null)
483                                 throw new HttpException ("Session state can only be used " +
484                                                 "when enableSessionState is set to true, either " +
485                                                 "in a configuration file or in the Page directive.");
486
487                         return _context.Session;
488                 }
489         }
490
491 #if NET_2_0
492         [FilterableAttribute (false)]
493         [Obsolete]
494 #endif
495         [Browsable (false)]
496         public bool SmartNavigation
497         {
498                 get { return _smartNavigation; }
499                 set { _smartNavigation = value; }
500         }
501
502 #if NET_2_0
503         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
504         [Filterable (false)]
505         [Browsable (false)]
506         public virtual string StyleSheetTheme {
507                 get { return _styleSheetTheme; }
508                 set { _styleSheetTheme = value; }
509         }
510
511         [Browsable (false)]
512         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
513         public virtual string Theme {
514                 get { return _theme; }
515                 set { _theme = value; }
516         }
517
518         void InitializeStyleSheet ()
519         {
520                 if (_styleSheetTheme == null) {
521                         PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
522                         if (ps != null)
523                                 _styleSheetTheme = ps.StyleSheetTheme;
524                 }
525                 if (_styleSheetTheme != null && _styleSheetTheme != "")
526                         _styleSheetPageTheme = ThemeDirectoryCompiler.GetCompiledInstance ("~/App_Themes/" + _styleSheetTheme + "/", _context);
527         }
528
529         void InitializeTheme ()
530         {
531                 if (_theme == null) {
532                         PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
533                         if (ps != null)
534                                 _theme = ps.Theme;
535                 }
536                 if (_theme != null && _theme != "") {
537                         _pageTheme = ThemeDirectoryCompiler.GetCompiledInstance ("~/App_Themes/" + _theme + "/", _context);
538                 }
539         }
540
541 #endif
542
543 #if NET_2_0
544         [Localizable (true)] 
545         [Bindable (true)] 
546         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
547         public string Title {
548                 get {
549                         if (_title == null)
550                                 return htmlHeader.Title;
551                         return _title;
552                 }
553                 set {
554                         if (htmlHeader != null)
555                                 htmlHeader.Title = value;
556                         else
557                                 _title = value;
558                 }
559         }
560 #endif
561
562         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
563         [Browsable (false)]
564         public TraceContext Trace
565         {
566                 get { return Context.Trace; }
567         }
568
569         [EditorBrowsable (EditorBrowsableState.Never)]
570 #if NET_2_0
571         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
572         [BrowsableAttribute (false)]
573         public bool TraceEnabled
574         {
575                 get { return Trace.IsEnabled; }
576                 set { Trace.IsEnabled = value; }
577         }
578 #else
579         protected bool TraceEnabled
580         {
581                 set { Trace.IsEnabled = value; }
582         }
583 #endif
584
585         [EditorBrowsable (EditorBrowsableState.Never)]
586 #if NET_2_0
587         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
588         [BrowsableAttribute (false)]
589         public TraceMode TraceModeValue
590         {
591                 get { return Trace.TraceMode; }
592                 set { Trace.TraceMode = value; }
593         }
594 #else
595         protected TraceMode TraceModeValue
596         {
597                 set { Trace.TraceMode = value; }
598         }
599 #endif
600
601         [EditorBrowsable (EditorBrowsableState.Never)]
602         protected int TransactionMode
603         {
604 #if NET_2_0
605                 get { return _transactionMode; }
606 #endif
607                 set { _transactionMode = value; }
608         }
609
610 #if NET_2_0
611         [EditorBrowsable (EditorBrowsableState.Advanced)]
612         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
613         [BrowsableAttribute (false)]
614         public string UICulture
615         {
616                 get { return Thread.CurrentThread.CurrentUICulture.Name; }
617                 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
618         }
619 #else
620         [EditorBrowsable (EditorBrowsableState.Never)]
621         protected string UICulture
622         {
623                 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
624         }
625 #endif
626
627         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
628         [Browsable (false)]
629         public IPrincipal User
630         {
631                 get { return Context.User; }
632         }
633
634         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
635         [Browsable (false)]
636         public ValidatorCollection Validators
637         {
638                 get { 
639                         if (_validators == null)
640                                 _validators = new ValidatorCollection ();
641                         return _validators;
642                 }
643         }
644
645         [MonoTODO ("Use this when encrypting/decrypting ViewState")]
646         [Browsable (false)]
647         public string ViewStateUserKey {
648                 get { return viewStateUserKey; }
649                 set { viewStateUserKey = value; }
650         }
651
652         [Browsable (false)]
653         public override bool Visible
654         {
655                 get { return base.Visible; }
656                 set { base.Visible = value; }
657         }
658
659         #endregion
660
661         #region Methods
662
663         [EditorBrowsable (EditorBrowsableState.Never)]
664         protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
665                                                              AsyncCallback cb, 
666                                                              object extraData)
667         {
668                 throw new NotImplementedException ();
669         }
670
671         [EditorBrowsable (EditorBrowsableState.Never)]
672         protected void AspCompatEndProcessRequest (IAsyncResult result)
673         {
674                 throw new NotImplementedException ();
675         }
676         
677         [EditorBrowsable (EditorBrowsableState.Advanced)]
678         protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
679         {
680                 return new HtmlTextWriter (tw);
681         }
682
683         [EditorBrowsable (EditorBrowsableState.Never)]
684         public void DesignerInitialize ()
685         {
686                 InitRecursive (null);
687         }
688
689         [EditorBrowsable (EditorBrowsableState.Advanced)]
690         protected virtual NameValueCollection DeterminePostBackMode ()
691         {
692                 if (_context == null)
693                         return null;
694
695                 HttpRequest req = _context.Request;
696                 if (req == null)
697                         return null;
698
699                 NameValueCollection coll = null;
700                 if (0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
701                         coll =  req.Form;
702                         WebROCollection c = (WebROCollection) coll;
703                         allow_load = !c.GotID;
704                         if (allow_load) {
705                                 c.ID = GetTypeHashCode ();
706                         } else {
707                                 allow_load = (c.ID == GetTypeHashCode ());
708                         }
709                 } else  {
710                         coll = req.QueryString;
711                 }
712
713                 if (coll != null && coll ["__VIEWSTATE"] == null && coll ["__EVENTTARGET"] == null)
714                         return null;
715
716                 return coll;
717         }
718
719 #if NET_2_0
720         public override Control FindControl (string id) {
721                 if (id == ID)
722                         return this;
723                 else
724                         return base.FindControl (id);
725         }
726 #endif
727
728 #if NET_2_0
729         [Obsolete]
730 #endif
731         [EditorBrowsable (EditorBrowsableState.Advanced)]
732         public string GetPostBackClientEvent (Control control, string argument)
733         {
734                 return scriptManager.GetPostBackEventReference (control, argument);
735         }
736
737 #if NET_2_0
738         [Obsolete]
739 #endif
740         [EditorBrowsable (EditorBrowsableState.Advanced)]
741         public string GetPostBackClientHyperlink (Control control, string argument)
742         {
743                 return scriptManager.GetPostBackClientHyperlink (control, argument);
744         }
745
746 #if NET_2_0
747         [Obsolete]
748 #endif
749         [EditorBrowsable (EditorBrowsableState.Advanced)]
750         public string GetPostBackEventReference (Control control)
751         {
752                 return scriptManager.GetPostBackEventReference (control, "");
753         }
754
755 #if NET_2_0
756         [Obsolete]
757 #endif
758         [EditorBrowsable (EditorBrowsableState.Advanced)]
759         public string GetPostBackEventReference (Control control, string argument)
760         {
761                 return scriptManager.GetPostBackEventReference (control, argument);
762         }
763
764         internal void RequiresPostBackScript ()
765         {
766                 requiresPostBackScript = true;
767         }
768
769         [EditorBrowsable (EditorBrowsableState.Never)]
770         public virtual int GetTypeHashCode ()
771         {
772                 return 0;
773         }
774
775 #if NET_2_0
776     [MonoTODO("The following properties of OutputCacheParameters are silently ignored: CacheProfile, NoStore, SqlDependency")]
777     protected internal virtual void InitOutputCache(OutputCacheParameters cacheSettings)
778     {
779         if (cacheSettings.Enabled)
780             InitOutputCache(cacheSettings.Duration,
781                 cacheSettings.VaryByHeader,
782                 cacheSettings.VaryByCustom,
783                 cacheSettings.Location,
784                 cacheSettings.VaryByParam);
785     }
786 #endif
787
788         [EditorBrowsable (EditorBrowsableState.Never)]
789         protected virtual void InitOutputCache (int duration,
790                                                 string varyByHeader,
791                                                 string varyByCustom,
792                                                 OutputCacheLocation location,
793                                                 string varyByParam)
794         {
795                 HttpCachePolicy cache = _context.Response.Cache;
796                 bool set_vary = false;
797
798                 switch (location) {
799                 case OutputCacheLocation.Any:
800                         cache.SetCacheability (HttpCacheability.Public);
801                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
802                         cache.SetLastModified (_context.Timestamp);
803                         set_vary = true;
804                         break;
805                 case OutputCacheLocation.Client:
806                         cache.SetCacheability (HttpCacheability.Private);
807                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
808                         cache.SetLastModified (_context.Timestamp);
809                         break;
810                 case OutputCacheLocation.Downstream:
811                         cache.SetCacheability (HttpCacheability.Public);
812                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
813                         cache.SetLastModified (_context.Timestamp);
814                         break;
815                 case OutputCacheLocation.Server:                        
816                         cache.SetCacheability (HttpCacheability.Server);
817                         set_vary = true;
818                         break;
819                 case OutputCacheLocation.None:
820                         break;
821                 }
822
823                 if (set_vary) {
824                         if (varyByCustom != null)
825                                 cache.SetVaryByCustom (varyByCustom);
826
827                         if (varyByParam != null && varyByParam.Length > 0) {
828                                 string[] prms = varyByParam.Split (';');
829                                 foreach (string p in prms)
830                                         cache.VaryByParams [p.Trim ()] = true;
831                                 cache.VaryByParams.IgnoreParams = false;
832                         } else {
833                                 cache.VaryByParams.IgnoreParams = true;
834                         }
835                         
836                         if (varyByHeader != null && varyByHeader.Length > 0) {
837                                 string[] hdrs = varyByHeader.Split (';');
838                                 foreach (string h in hdrs)
839                                         cache.VaryByHeaders [h.Trim ()] = true;
840                         }
841                 }
842                         
843                 cache.Duration = duration;
844                 cache.SetExpires (_context.Timestamp.AddSeconds (duration));
845         }
846
847 #if NET_2_0
848         [Obsolete]
849 #else
850         [EditorBrowsable (EditorBrowsableState.Advanced)]
851 #endif
852         public bool IsClientScriptBlockRegistered (string key)
853         {
854                 return scriptManager.IsClientScriptBlockRegistered (key);
855         }
856
857 #if NET_2_0
858         [Obsolete]
859 #else
860         [EditorBrowsable (EditorBrowsableState.Advanced)]
861 #endif
862         public bool IsStartupScriptRegistered (string key)
863         {
864                 return scriptManager.IsStartupScriptRegistered (key);
865         }
866
867         public string MapPath (string virtualPath)
868         {
869                 return Request.MapPath (virtualPath);
870         }
871
872 #if NET_2_0
873         protected internal override void Render (HtmlTextWriter writer) {
874                 if (MaintainScrollPositionOnPostBack) {
875                         RequiresPostBackScript ();
876
877                         string scriptUrl = ClientScript.GetWebResourceUrl (typeof (Page), "MaintainScrollPositionOnPostBack.js");
878
879                         ClientScript.RegisterClientScriptInclude (typeof (Page), "MaintainScrollPositionOnPostBack.js", scriptUrl);
880
881                         ClientScript.RegisterHiddenField (ScrollPositionXID, Request [ScrollPositionXID]);
882                         ClientScript.RegisterHiddenField (ScrollPositionYID, Request [ScrollPositionYID]);
883                         
884                         StringBuilder script = new StringBuilder ();
885                         script.AppendLine ("<script type=\"text/javascript\">");
886                         script.AppendLine ("<!--");
887                         script.AppendLine ("theForm.oldSubmit = theForm.submit");
888                         script.AppendLine ("theForm.submit = WebForm_SaveScrollPositionSubmit");
889                         script.AppendLine ("theForm.oldOnSubmit = theForm.onsubmit");
890                         script.AppendLine ("theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit");
891                         if (IsPostBack) {
892                                 script.AppendLine ("theForm.oldOnLoad = window.onload");
893                                 script.AppendLine ("window.onload = WebForm_RestoreScrollPosition");
894                         }
895                         script.AppendLine ("// -->");
896                         script.AppendLine ("</script>");
897                         
898                         ClientScript.RegisterStartupScript (typeof (Page), "MaintainScrollPositionOnPostBackStartup", script.ToString());
899                 }
900                 base.Render (writer);
901         }
902 #endif
903
904         private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
905         {
906                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
907                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
908                 writer.WriteLine ();
909                 writer.WriteLine ("<script language=\"javascript\">");
910                 writer.WriteLine ("<!--");
911
912                 writer.WriteLine ("\tvar theForm;\n\tif (document.getElementById) {{ theForm = document.getElementById ('{0}'); }}", formUniqueID);
913                 writer.WriteLine ("\telse {{ theForm = document.{0}; }}", formUniqueID);
914                 writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
915                 writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
916                 writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
917                 writer.WriteLine ("\t\ttheForm.submit();");
918                 writer.WriteLine ("\t}");
919                 writer.WriteLine ("// -->");
920                 writer.WriteLine ("</script>");
921         }
922
923         internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
924         {
925                 if (renderingForm)
926                         throw new HttpException ("Only 1 HtmlForm is allowed per page.");
927
928                 renderingForm = true;
929                 writer.WriteLine ();
930                 scriptManager.WriteHiddenFields (writer);
931                 if (requiresPostBackScript) {
932                         RenderPostBackScript (writer, formUniqueID);
933                         postBackScriptRendered = true;
934                 }
935
936                 if (handleViewState) {
937                         string vs = GetViewStateString ();
938                         writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
939                         writer.WriteLine ("value=\"{0}\" />", vs);
940                 }
941
942                 scriptManager.WriteClientScriptBlocks (writer);
943         }
944
945         LosFormatter GetFormatter ()
946         {
947 #if NET_2_0
948                 PagesSection config = (PagesSection) WebConfigurationManager.GetSection ("system.web/pages");
949 #else
950                 PagesConfiguration config = PagesConfiguration.GetInstance (_context);
951 #endif
952                 byte [] vkey = null;
953                 if (config.EnableViewStateMac) {
954 #if NET_2_0
955                         MachineKeySection mconfig = (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey");
956                         vkey = mconfig.ValidationKeyBytes;
957 #else
958                         MachineKeyConfig mconfig = HttpContext.GetAppConfig ("system.web/machineKey") as MachineKeyConfig;
959                         vkey = mconfig.ValidationKey;
960 #endif
961                 }
962
963                 return new LosFormatter (config.EnableViewStateMac, vkey);
964         }
965
966         string GetViewStateString ()
967         {
968                 if (_savedViewState == null)
969                         return null;
970
971                 LosFormatter fmt = GetFormatter ();
972                 return fmt.SerializeToBase64 (_savedViewState);
973         }
974
975         internal object GetSavedViewState ()
976         {
977                 return _savedViewState;
978         }
979
980         internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
981         {
982                 scriptManager.WriteArrayDeclares (writer);
983
984                 if (!postBackScriptRendered && requiresPostBackScript)
985                         RenderPostBackScript (writer, formUniqueID);
986
987                 scriptManager.WriteHiddenFields (writer);
988                 scriptManager.WriteClientScriptIncludes (writer);
989                 scriptManager.WriteStartupScriptBlocks (writer);
990                 renderingForm = false;
991                 postBackScriptRendered = false;
992         }
993
994         private void ProcessPostData (NameValueCollection data, bool second)
995         {
996                 if (data == null)
997                         return;
998
999                 if (_requiresPostBackCopy == null && _requiresPostBack != null)
1000                         _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
1001
1002                 Hashtable used = new Hashtable ();
1003                 foreach (string id in data.AllKeys){
1004                         if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
1005                                 continue;
1006
1007                         string real_id = id;
1008                         int dot = real_id.IndexOf ('.');
1009                         if (dot >= 1)
1010                                 real_id = real_id.Substring (0, dot);
1011                         
1012                         if (real_id == null || used.ContainsKey (real_id))
1013                                 continue;
1014
1015                         used.Add (real_id, real_id);
1016
1017                         Control ctrl = FindControl (real_id);
1018                         if (ctrl != null){
1019                                 IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
1020                                 IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
1021
1022                                 if (pbdh == null) {
1023                                         if (pbeh != null)
1024                                                 RegisterRequiresRaiseEvent (pbeh);
1025                                         continue;
1026                                 }
1027                 
1028                                 if (pbdh.LoadPostData (real_id, data) == true) {
1029                                         if (requiresPostDataChanged == null)
1030                                                 requiresPostDataChanged = new ArrayList ();
1031                                         requiresPostDataChanged.Add (pbdh);
1032                                         if (_requiresPostBackCopy != null)
1033                                                 _requiresPostBackCopy.Remove (ctrl.UniqueID);
1034                                 }
1035                         } else if (!second) {
1036                                 if (secondPostData == null)
1037                                         secondPostData = new NameValueCollection ();
1038                                 secondPostData.Add (real_id, data [id]);
1039                         }
1040                 }
1041
1042                 ArrayList list1 = null;
1043                 if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
1044                         string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
1045                         foreach (string id in handlers) {
1046                                 IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
1047                                 if (pbdh != null) {                     
1048                                         _requiresPostBackCopy.Remove (id);
1049                                         if (pbdh.LoadPostData (id, data)) {
1050                                                 if (requiresPostDataChanged == null)
1051                                                         requiresPostDataChanged = new ArrayList ();
1052         
1053                                                 requiresPostDataChanged.Add (pbdh);
1054                                         }
1055                                 } else if (second) {
1056                                         if (list1 == null)
1057                                                 list1 = new ArrayList ();
1058                                         list1.Add (id);
1059                                 }
1060                         }
1061                 }
1062                 _requiresPostBack = list1;
1063         }
1064
1065         [EditorBrowsable (EditorBrowsableState.Never)]
1066 #if NET_2_0 || TARGET_JVM
1067         public virtual void ProcessRequest (HttpContext context)
1068 #else
1069         public void ProcessRequest (HttpContext context)
1070 #endif
1071         {
1072                 _context = context;
1073                 if (clientTarget != null)
1074                         Request.ClientTarget = clientTarget;
1075
1076                 WireupAutomaticEvents ();
1077                 //-- Control execution lifecycle in the docs
1078
1079                 // Save culture information because it can be modified in FrameworkInitialize()
1080                 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
1081                 CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
1082                 FrameworkInitialize ();
1083                 context.ErrorPage = _errorPage;
1084
1085                 try {
1086                         InternalProcessRequest ();
1087                 } catch (ThreadAbortException) {
1088                         // Do nothing, just ignore it by now.
1089                 } catch (Exception e) {
1090                         context.AddError (e); // OnError might access LastError
1091                         OnError (EventArgs.Empty);
1092                         context.ClearError (e);
1093                         // We want to remove that error, as we're rethrowing to stop
1094                         // further processing.
1095                         Trace.Warn ("Unhandled Exception", e.ToString (), e);
1096                         throw;
1097                 } finally {
1098                         try {
1099                                 RenderTrace ();
1100                                 UnloadRecursive (true);
1101                         } catch {}
1102                         if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
1103                                 Thread.CurrentThread.CurrentCulture = culture;
1104
1105                         if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
1106                                 Thread.CurrentThread.CurrentUICulture = uiculture;
1107                 }
1108         }
1109         
1110 #if NET_2_0
1111         internal void ProcessCrossPagePostBack (HttpContext context)
1112         {
1113                 isCrossPagePostBack = true;
1114                 ProcessRequest (context);
1115         }
1116 #endif
1117
1118         void InternalProcessRequest ()
1119         {
1120                 _requestValueCollection = this.DeterminePostBackMode();
1121
1122 #if NET_2_0
1123                 // http://msdn2.microsoft.com/en-us/library/ms178141.aspx
1124                 if (_requestValueCollection != null) {
1125                         if (!isCrossPagePostBack && _requestValueCollection [PreviousPageID] != null && _requestValueCollection [PreviousPageID] != Request.FilePath) {
1126                                 LoadPreviousPageReference ();
1127                         }
1128                         else {
1129                                 isCallback = _requestValueCollection [CallbackArgumentID] != null;
1130                                 isPostBack = !isCallback;
1131                         }
1132                 }
1133
1134                 OnPreInit (EventArgs.Empty);
1135
1136                 InitializeTheme ();
1137                 ApplyMasterPage ();
1138 #endif
1139                 Trace.Write ("aspx.page", "Begin Init");
1140                 InitRecursive (null);
1141                 Trace.Write ("aspx.page", "End Init");
1142
1143 #if NET_2_0
1144                 OnInitComplete (EventArgs.Empty);
1145 #endif
1146                         
1147                 renderingForm = false;  
1148                 if (IsPostBack) {
1149                         Trace.Write ("aspx.page", "Begin LoadViewState");
1150                         LoadPageViewState ();
1151                         Trace.Write ("aspx.page", "End LoadViewState");
1152                         Trace.Write ("aspx.page", "Begin ProcessPostData");
1153                         ProcessPostData (_requestValueCollection, false);
1154                         Trace.Write ("aspx.page", "End ProcessPostData");
1155                 }
1156
1157 #if NET_2_0
1158                 OnPreLoad (EventArgs.Empty);
1159 #endif
1160
1161                 LoadRecursive ();
1162                 if (IsPostBack) {
1163                         Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
1164                         ProcessPostData (secondPostData, true);
1165                         Trace.Write ("aspx.page", "End ProcessPostData Second Try");
1166                         Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
1167                         RaiseChangedEvents ();
1168                         Trace.Write ("aspx.page", "End Raise ChangedEvents");
1169                         Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
1170                         RaisePostBackEvents ();
1171                         Trace.Write ("aspx.page", "End Raise PostBackEvent");
1172                 }
1173                 
1174 #if NET_2_0
1175                 OnLoadComplete (EventArgs.Empty);
1176
1177                 if (IsCrossPagePostBack)
1178                         return;
1179
1180                 if (IsCallback) {
1181                         string result = ProcessCallbackData ();
1182                         HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
1183                         callbackOutput.Write (result);
1184                         callbackOutput.Flush ();
1185                         return;
1186                 }
1187 #endif
1188                 
1189                 Trace.Write ("aspx.page", "Begin PreRender");
1190                 PreRenderRecursiveInternal ();
1191                 Trace.Write ("aspx.page", "End PreRender");
1192                 
1193 #if NET_2_0
1194                 OnPreRenderComplete (EventArgs.Empty);
1195 #endif
1196
1197                 Trace.Write ("aspx.page", "Begin SaveViewState");
1198                 SavePageViewState ();
1199                 Trace.Write ("aspx.page", "End SaveViewState");
1200                 
1201 #if NET_2_0
1202                 OnSaveStateComplete (EventArgs.Empty);
1203 #endif
1204                 
1205                 //--
1206                 Trace.Write ("aspx.page", "Begin Render");
1207                 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1208                 RenderControl (output);
1209                 Trace.Write ("aspx.page", "End Render");
1210         }
1211
1212         private void RenderTrace ()
1213         {
1214                 TraceManager traceManager = HttpRuntime.TraceManager;
1215
1216                 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
1217                         return;
1218                 
1219                 Trace.SaveData ();
1220
1221                 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput) 
1222                         return;
1223
1224                 if (!traceManager.LocalOnly || Context.Request.IsLocal) {
1225                         HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1226                         Trace.Render (output);
1227                 }
1228         }
1229         
1230         void RaisePostBackEvents ()
1231         {
1232                 if (requiresRaiseEvent != null) {
1233                         RaisePostBackEvent (requiresRaiseEvent, null);
1234                         return;
1235                 }
1236
1237                 NameValueCollection postdata = _requestValueCollection;
1238                 if (postdata == null)
1239                         return;
1240
1241                 string eventTarget = postdata [postEventSourceID];
1242                 if (eventTarget == null || eventTarget.Length == 0) {
1243                         Validate ();
1244                         return;
1245                 }
1246
1247                 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
1248                 if (target == null)
1249                         return;
1250
1251                 string eventArgument = postdata [postEventArgumentID];
1252                 RaisePostBackEvent (target, eventArgument);
1253         }
1254
1255         internal void RaiseChangedEvents ()
1256         {
1257                 if (requiresPostDataChanged == null)
1258                         return;
1259
1260                 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
1261                         ipdh.RaisePostDataChangedEvent ();
1262
1263                 requiresPostDataChanged = null;
1264         }
1265
1266         [EditorBrowsable (EditorBrowsableState.Advanced)]
1267         protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
1268         {
1269                 sourceControl.RaisePostBackEvent (eventArgument);
1270         }
1271         
1272 #if NET_2_0
1273         [Obsolete]
1274 #endif
1275         [EditorBrowsable (EditorBrowsableState.Advanced)]
1276         public void RegisterArrayDeclaration (string arrayName, string arrayValue)
1277         {
1278                 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
1279         }
1280
1281 #if NET_2_0
1282         [Obsolete]
1283 #endif
1284         [EditorBrowsable (EditorBrowsableState.Advanced)]
1285         public virtual void RegisterClientScriptBlock (string key, string script)
1286         {
1287                 scriptManager.RegisterClientScriptBlock (key, script);
1288         }
1289
1290 #if NET_2_0
1291         [Obsolete]
1292 #endif
1293         [EditorBrowsable (EditorBrowsableState.Advanced)]
1294         public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
1295         {
1296                 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
1297         }
1298
1299         [MonoTODO("Used in HtmlForm")]
1300         internal void RegisterClientScriptFile (string a, string b, string c)
1301         {
1302                 throw new NotImplementedException ();
1303         }
1304
1305 #if NET_2_0
1306         [Obsolete]
1307 #endif
1308         [EditorBrowsable (EditorBrowsableState.Advanced)]
1309         public void RegisterOnSubmitStatement (string key, string script)
1310         {
1311                 scriptManager.RegisterOnSubmitStatement (key, script);
1312         }
1313
1314         internal string GetSubmitStatements ()
1315         {
1316                 return scriptManager.WriteSubmitStatements ();
1317         }
1318
1319         [EditorBrowsable (EditorBrowsableState.Advanced)]
1320         public void RegisterRequiresPostBack (Control control)
1321         {
1322                 if (_requiresPostBack == null)
1323                         _requiresPostBack = new ArrayList ();
1324
1325                 _requiresPostBack.Add (control.UniqueID);
1326         }
1327
1328         [EditorBrowsable (EditorBrowsableState.Advanced)]
1329         public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
1330         {
1331                 requiresRaiseEvent = control;
1332         }
1333
1334 #if NET_2_0
1335         [Obsolete]
1336 #endif
1337         [EditorBrowsable (EditorBrowsableState.Advanced)]
1338         public virtual void RegisterStartupScript (string key, string script)
1339         {
1340                 scriptManager.RegisterStartupScript (key, script);
1341         }
1342
1343         [EditorBrowsable (EditorBrowsableState.Advanced)]
1344         public void RegisterViewStateHandler ()
1345         {
1346                 handleViewState = true;
1347         }
1348
1349         [EditorBrowsable (EditorBrowsableState.Advanced)]
1350         protected virtual void SavePageStateToPersistenceMedium (object viewState)
1351         {
1352                 _savedViewState = viewState;
1353         }
1354
1355         [EditorBrowsable (EditorBrowsableState.Advanced)]
1356         protected virtual object LoadPageStateFromPersistenceMedium ()
1357         {
1358                 NameValueCollection postdata = _requestValueCollection;
1359                 string view_state;
1360                 if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
1361                         return null;
1362
1363                 _savedViewState = null;
1364                 if (view_state == "")
1365                         return null;
1366
1367                 LosFormatter fmt = GetFormatter ();
1368                 try {
1369                         _savedViewState = fmt.Deserialize (view_state);
1370                 } catch (Exception e) {
1371                         throw new HttpException ("Error restoring page viewstate.", e);
1372                 }
1373                 return _savedViewState;
1374         }
1375
1376         internal void LoadPageViewState()
1377         {
1378                 object sState = LoadPageStateFromPersistenceMedium ();
1379                 if (sState != null) {
1380 #if NET_2_0
1381                         Triplet data = (Triplet) sState;
1382                         if (allow_load) {
1383                                 LoadPageControlState (data.Third);
1384                                 LoadViewStateRecursive (data.First);
1385                                 _requiresPostBack = data.Second as ArrayList;
1386                         }
1387 #else
1388                         Pair pair = (Pair) sState;
1389                         if (allow_load) {
1390                                 LoadViewStateRecursive (pair.First);
1391                                 _requiresPostBack = pair.Second as ArrayList;
1392                         }
1393 #endif
1394                 }
1395         }
1396
1397         internal void SavePageViewState ()
1398         {
1399                 if (!handleViewState)
1400                         return;
1401
1402 #if NET_2_0
1403                 object controlState = SavePageControlState ();
1404 #endif
1405
1406                 object viewState = SaveViewStateRecursive ();
1407                 object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
1408
1409 #if NET_2_0
1410                 Triplet triplet = new Triplet ();
1411                 triplet.First = viewState;
1412                 triplet.Second = reqPostback;
1413                 triplet.Third = controlState;
1414
1415                 if (triplet.First == null && triplet.Second == null && triplet.Third == null)
1416                         triplet = null;
1417                         
1418                 SavePageStateToPersistenceMedium (triplet);
1419 #else
1420                 Pair pair = new Pair ();
1421                 pair.First = viewState;
1422                 pair.Second = reqPostback;
1423
1424                 if (pair.First == null && pair.Second == null)
1425                         pair = null;
1426                         
1427                 SavePageStateToPersistenceMedium (pair);
1428 #endif
1429         }
1430
1431         public virtual void Validate ()
1432         {
1433                 is_validated = true;
1434                 ValidateCollection (_validators);
1435         }
1436
1437 #if NET_2_0
1438         internal bool AreValidatorsUplevel () {
1439                 return AreValidatorsUplevel (String.Empty);
1440         }
1441
1442         internal bool AreValidatorsUplevel (string valGroup)
1443 #else
1444         internal virtual bool AreValidatorsUplevel ()
1445 #endif
1446         {
1447                 bool uplevel = false;
1448
1449                 foreach (IValidator v in Validators) {
1450                         BaseValidator bv = v as BaseValidator;
1451                         if (bv == null) continue;
1452
1453 #if NET_2_0
1454                         if (valGroup != bv.ValidationGroup)
1455                                 continue;
1456 #endif
1457                         if (bv.GetRenderUplevel()) {
1458                                 uplevel = true;
1459                                 break;
1460                         }
1461                 }
1462
1463                 return uplevel;
1464         }
1465
1466         bool ValidateCollection (ValidatorCollection validators)
1467         {
1468 #if NET_2_0
1469                 if (!_eventValidation)
1470                         return true;
1471 #endif
1472
1473                 if (validators == null || validators.Count == 0)
1474                         return true;
1475
1476                 bool all_valid = true;
1477                 foreach (IValidator v in validators){
1478                         v.Validate ();
1479                         if (v.IsValid == false)
1480                                 all_valid = false;
1481                 }
1482
1483                 return all_valid;
1484         }
1485
1486         [EditorBrowsable (EditorBrowsableState.Advanced)]
1487         public virtual void VerifyRenderingInServerForm (Control control)
1488         {
1489                 if (_context == null)
1490                         return;
1491
1492                 if (!renderingForm)
1493                         throw new HttpException ("Control '" +
1494                                                  control.ClientID +
1495                                                  "' of type '" +
1496                                                  control.GetType ().Name +
1497                                                  "' must be placed inside a form tag with runat=server.");
1498         }
1499
1500         protected override void FrameworkInitialize ()
1501         {
1502                 base.FrameworkInitialize ();
1503 #if NET_2_0
1504                 InitializeStyleSheet ();
1505 #endif
1506         }
1507
1508         #endregion
1509         
1510         #if NET_2_0
1511         public
1512         #else
1513         internal
1514         #endif
1515                 ClientScriptManager ClientScript {
1516                 get { return scriptManager; }
1517         }
1518         
1519         #if NET_2_0
1520         
1521         static readonly object InitCompleteEvent = new object ();
1522         static readonly object LoadCompleteEvent = new object ();
1523         static readonly object PreInitEvent = new object ();
1524         static readonly object PreLoadEvent = new object ();
1525         static readonly object PreRenderCompleteEvent = new object ();
1526         static readonly object SaveStateCompleteEvent = new object ();
1527         int event_mask;
1528         const int initcomplete_mask = 1;
1529         const int loadcomplete_mask = 1 << 1;
1530         const int preinit_mask = 1 << 2;
1531         const int preload_mask = 1 << 3;
1532         const int prerendercomplete_mask = 1 << 4;
1533         const int savestatecomplete_mask = 1 << 5;
1534         
1535         [EditorBrowsable (EditorBrowsableState.Advanced)]
1536         public event EventHandler InitComplete {
1537                 add {
1538                         event_mask |= initcomplete_mask;
1539                         Events.AddHandler (InitCompleteEvent, value);
1540                 }
1541                 remove { Events.RemoveHandler (InitCompleteEvent, value); }
1542         }
1543         
1544         [EditorBrowsable (EditorBrowsableState.Advanced)]
1545         public event EventHandler LoadComplete {
1546                 add {
1547                         event_mask |= loadcomplete_mask;
1548                         Events.AddHandler (LoadCompleteEvent, value);
1549                 }
1550                 remove { Events.RemoveHandler (LoadCompleteEvent, value); }
1551         }
1552         
1553         public event EventHandler PreInit {
1554                 add {
1555                         event_mask |= preinit_mask;
1556                         Events.AddHandler (PreInitEvent, value);
1557                 }
1558                 remove { Events.RemoveHandler (PreInitEvent, value); }
1559         }
1560         
1561         [EditorBrowsable (EditorBrowsableState.Advanced)]
1562         public event EventHandler PreLoad {
1563                 add {
1564                         event_mask |= preload_mask;
1565                         Events.AddHandler (PreLoadEvent, value);
1566                 }
1567                 remove { Events.RemoveHandler (PreLoadEvent, value); }
1568         }
1569         
1570         [EditorBrowsable (EditorBrowsableState.Advanced)]
1571         public event EventHandler PreRenderComplete {
1572                 add {
1573                         event_mask |= prerendercomplete_mask;
1574                         Events.AddHandler (PreRenderCompleteEvent, value);
1575                 }
1576                 remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
1577         }
1578         
1579         [EditorBrowsable (EditorBrowsableState.Advanced)]
1580         public event EventHandler SaveStateComplete {
1581                 add {
1582                         event_mask |= savestatecomplete_mask;
1583                         Events.AddHandler (SaveStateCompleteEvent, value);
1584                 }
1585                 remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
1586         }
1587         
1588         protected virtual void OnInitComplete (EventArgs e)
1589         {
1590                 if ((event_mask & initcomplete_mask) != 0) {
1591                         EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
1592                         if (eh != null) eh (this, e);
1593                 }
1594         }
1595         
1596         protected virtual void OnLoadComplete (EventArgs e)
1597         {
1598                 if ((event_mask & loadcomplete_mask) != 0) {
1599                         EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
1600                         if (eh != null) eh (this, e);
1601                 }
1602         }
1603         
1604         protected virtual void OnPreInit (EventArgs e)
1605         {
1606                 if ((event_mask & preinit_mask) != 0) {
1607                         EventHandler eh = (EventHandler) (Events [PreInitEvent]);
1608                         if (eh != null) eh (this, e);
1609                 }
1610         }
1611         
1612         protected virtual void OnPreLoad (EventArgs e)
1613         {
1614                 if ((event_mask & preload_mask) != 0) {
1615                         EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
1616                         if (eh != null) eh (this, e);
1617                 }
1618         }
1619         
1620         protected virtual void OnPreRenderComplete (EventArgs e)
1621         {
1622                 if ((event_mask & prerendercomplete_mask) != 0) {
1623                         EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
1624                         if (eh != null) eh (this, e);
1625                 }
1626         }
1627         
1628         protected virtual void OnSaveStateComplete (EventArgs e)
1629         {
1630                 if ((event_mask & savestatecomplete_mask) != 0) {
1631                         EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
1632                         if (eh != null) eh (this, e);
1633                 }
1634         }
1635         
1636         public HtmlForm Form {
1637                 get { return _form; }
1638         }
1639         
1640         internal void RegisterForm (HtmlForm form)
1641         {
1642                 _form = form;
1643         }
1644         
1645         [BrowsableAttribute (false)]
1646         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1647         public Page PreviousPage {
1648                 get { return previousPage; }
1649         }
1650
1651         
1652         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1653         [BrowsableAttribute (false)]
1654         public bool IsCallback {
1655                 get { return isCallback; }
1656         }
1657         
1658         [BrowsableAttribute (false)]
1659         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1660         public bool IsCrossPagePostBack {
1661                 get { return isCrossPagePostBack; }
1662         }
1663         
1664         string ProcessCallbackData ()
1665         {
1666                 string callbackTarget = _requestValueCollection [CallbackSourceID];
1667                 if (callbackTarget == null || callbackTarget.Length == 0)
1668                         throw new HttpException ("Callback target not provided.");
1669
1670                 ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
1671                 if (target == null)
1672                         throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
1673
1674                 string callbackArgument = _requestValueCollection [CallbackArgumentID];
1675                 target.RaiseCallbackEvent (callbackArgument);
1676                 return target.GetCallbackResult ();
1677         }
1678
1679         [BrowsableAttribute (false)]
1680         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1681         public HtmlHead Header {
1682                 get { return htmlHeader; }
1683         }
1684         
1685         internal void SetHeader (HtmlHead header)
1686         {
1687                 htmlHeader = header;
1688                 if (_title != null) {
1689                         htmlHeader.Title = _title;
1690                         _title = null;
1691                 }
1692         }
1693         
1694         void ApplyMasterPage ()
1695         {
1696                 if (masterPageFile != null && masterPageFile.Length > 0) {
1697                         ArrayList appliedMasterPageFiles = new ArrayList ();
1698
1699                         if (Master != null) {
1700                                 MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
1701
1702                                 Master.Page = this;
1703                                 Controls.Clear ();
1704                                 Controls.Add (Master);
1705                         }
1706                 }
1707         }
1708
1709         [DefaultValueAttribute ("")]
1710         public virtual string MasterPageFile {
1711                 get { return masterPageFile; }
1712                 set {
1713                         masterPageFile = value;
1714                         masterPage = null;
1715                 }
1716         }
1717         
1718         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1719         [BrowsableAttribute (false)]
1720         public MasterPage Master {
1721                 get {
1722                         if (masterPage == null)
1723                                 masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
1724
1725                         return masterPage;
1726                 }
1727         }
1728         
1729         [EditorBrowsable (EditorBrowsableState.Advanced)]
1730         public void RegisterRequiresControlState (Control control)
1731         {
1732                 if (requireStateControls == null) requireStateControls = new ArrayList ();
1733                 requireStateControls.Add (control);
1734         }
1735         
1736         public bool RequiresControlState (Control control)
1737         {
1738                 if (requireStateControls == null) return false;
1739                 return requireStateControls.Contains (control);
1740         }
1741         
1742         [EditorBrowsable (EditorBrowsableState.Advanced)]
1743         public void UnregisterRequiresControlState (Control control)
1744         {
1745                 if (requireStateControls != null)
1746                         requireStateControls.Remove (control);
1747         }
1748         
1749         public ValidatorCollection GetValidators (string validationGroup)
1750         {
1751                 string valgr = validationGroup;
1752                 if (valgr == null)
1753                         valgr = String.Empty;
1754
1755                 if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
1756                 ValidatorCollection col = _validatorsByGroup [valgr] as ValidatorCollection;
1757                 if (col == null) {
1758                         col = new ValidatorCollection ();
1759                         _validatorsByGroup [valgr] = col;
1760                 }
1761                 return col;
1762         }
1763         
1764         public virtual void Validate (string validationGroup)
1765         {
1766                 is_validated = true;
1767                 if (validationGroup == null)
1768                         ValidateCollection (_validatorsByGroup [String.Empty] as ValidatorCollection);
1769                 else if (_validatorsByGroup != null) {
1770                         ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
1771                 }
1772         }
1773
1774         object SavePageControlState ()
1775         {
1776                 if (requireStateControls == null) return null;
1777                 object[] state = new object [requireStateControls.Count];
1778                 
1779                 bool allNull = true;
1780                 for (int n=0; n<state.Length; n++) {
1781                         state [n] = ((Control) requireStateControls [n]).SaveControlState ();
1782                         if (state [n] != null) allNull = false;
1783                 }
1784                 if (allNull) return null;
1785                 else return state;
1786         }
1787         
1788         void LoadPageControlState (object data)
1789         {
1790                 if (requireStateControls == null) return;
1791
1792                 object[] state = (object[]) data;
1793                 int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
1794                 for (int n=0; n < max; n++) {
1795                         Control ctl = (Control) requireStateControls [n];
1796                         ctl.LoadControlState (state != null ? state [n] : null);
1797                 }
1798         }
1799
1800         void LoadPreviousPageReference ()
1801         {
1802                 if (_requestValueCollection != null) {
1803                         string prevPage = _requestValueCollection [PreviousPageID];
1804                         if (prevPage != null) {
1805                                 previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
1806                                 previousPage.ProcessCrossPagePostBack (_context);
1807                         } else {
1808                                 previousPage = _context.LastPage;
1809                         }
1810                 }
1811                 _context.LastPage = this;
1812         }
1813
1814
1815         Hashtable contentTemplates;
1816         [EditorBrowsable (EditorBrowsableState.Never)]
1817         protected internal void AddContentTemplate (string templateName, ITemplate template)
1818         {
1819                 if (contentTemplates == null)
1820                         contentTemplates = new Hashtable ();
1821                 contentTemplates [templateName] = template;
1822         }
1823
1824         PageTheme _pageTheme;
1825         internal PageTheme PageTheme {
1826                 get { return _pageTheme; }
1827         }
1828
1829         PageTheme _styleSheetPageTheme;
1830         internal PageTheme StyleSheetPageTheme {
1831                 get { return _styleSheetPageTheme; }
1832         }
1833
1834         Stack dataItemCtx;
1835         
1836         internal void PushDataItemContext (object o) {
1837                 if (dataItemCtx == null)
1838                         dataItemCtx = new Stack ();
1839                 
1840                 dataItemCtx.Push (o);
1841         }
1842         
1843         internal void PopDataItemContext () {
1844                 if (dataItemCtx == null)
1845                         throw new InvalidOperationException ();
1846                 
1847                 dataItemCtx.Pop ();
1848         }
1849         
1850         public object GetDataItem() {
1851                 if (dataItemCtx == null || dataItemCtx.Count == 0)
1852                         throw new InvalidOperationException ("No data item");
1853                 
1854                 return dataItemCtx.Peek ();
1855         }
1856
1857         protected internal override void OnInit (EventArgs e)
1858         {
1859                 base.OnInit (e);
1860                 if (Header == null)
1861                         return;
1862
1863                 ArrayList themes = new ArrayList();
1864
1865                 if (StyleSheetPageTheme != null && StyleSheetPageTheme.GetStyleSheets () != null)
1866                         themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
1867                 if (PageTheme != null && PageTheme.GetStyleSheets () != null)
1868                         themes.AddRange (PageTheme.GetStyleSheets ());
1869
1870                 foreach (string lss in themes) {
1871                         HtmlLink hl = new HtmlLink ();
1872                         hl.Href = ResolveUrl (lss);
1873                         hl.Attributes["type"] = "text/css";
1874                         hl.Attributes["rel"] = "stylesheet";
1875                         Header.Controls.Add (hl);
1876                 }
1877         }
1878
1879         #endif
1880
1881 #if NET_2_0
1882         [MonoTODO ("Used by .net aspx parser")]
1883         protected object GetWrappedFileDependencies (string [] list)
1884         {
1885                 return null;
1886         }
1887
1888         [MonoTODO ("Used by .net aspx parser")]
1889         protected virtual void InitializeCulture ()
1890         {
1891         }
1892
1893         [MonoTODO ("Used by .net aspx parser")]
1894         protected internal void AddWrappedFileDependencies (object virtualFileDependencies)
1895         {
1896         }
1897 #endif
1898 }
1899 }