* MasterPage.cs: Include relative URL of MasterPage in exception
[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 using System.Collections.Generic;
55 using System.Reflection;
56 #endif
57
58 namespace System.Web.UI
59 {
60 // CAS
61 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
62 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
63 #if !NET_2_0
64 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
65 #endif
66 [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
67 [ToolboxItem (false)]
68 #if NET_2_0
69 [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
70 #else
71 [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
72 #endif
73 public partial class Page : TemplateControl, IHttpHandler
74 {
75         static string machineKeyConfigPath = "system.web/machineKey";
76 #if NET_2_0
77         private PageLifeCycle _lifeCycle = PageLifeCycle.Unknown;
78         private bool _eventValidation = true;
79         private object [] _savedControlState;
80         private bool _doLoadPreviousPage;
81         string _focusedControlID;
82         bool _hasEnabledControlArray;
83 #endif
84         private bool _viewState = true;
85         private bool _viewStateMac;
86         private string _errorPage;
87         private bool is_validated;
88         private bool _smartNavigation;
89         private int _transactionMode;
90         private HttpContext _context;
91         private ValidatorCollection _validators;
92         private bool renderingForm;
93         private string _savedViewState;
94         private ArrayList _requiresPostBack;
95         private ArrayList _requiresPostBackCopy;
96         private ArrayList requiresPostDataChanged;
97         private IPostBackEventHandler requiresRaiseEvent;
98         private NameValueCollection secondPostData;
99         private bool requiresPostBackScript;
100         private bool postBackScriptRendered;
101         bool handleViewState;
102         string viewStateUserKey;
103         NameValueCollection _requestValueCollection;
104         string clientTarget;
105         ClientScriptManager scriptManager;
106         bool allow_load; // true when the Form collection belongs to this page (GetTypeHashCode)
107         PageStatePersister page_state_persister;
108
109         [EditorBrowsable (EditorBrowsableState.Never)]
110 #if NET_2_0
111         public
112 #else
113         protected
114 #endif
115         const string postEventArgumentID = "__EVENTARGUMENT";
116
117         [EditorBrowsable (EditorBrowsableState.Never)]
118 #if NET_2_0
119         public
120 #else
121         protected
122 #endif
123         const string postEventSourceID = "__EVENTTARGET";
124
125 #if NET_2_0
126         const string ScrollPositionXID = "__SCROLLPOSITIONX";
127         const string ScrollPositionYID = "__SCROLLPOSITIONY";
128         const string EnabledControlArrayID = "__enabledControlArray";
129 #endif
130
131 #if NET_2_0
132         internal const string LastFocusID = "__LASTFOCUS";
133         internal const string CallbackArgumentID = "__CALLBACKARGUMENT";
134         internal const string CallbackSourceID = "__CALLBACKTARGET";
135         internal const string PreviousPageID = "__PREVIOUSPAGE";
136
137         HtmlHead htmlHeader;
138         
139         MasterPage masterPage;
140         string masterPageFile;
141         
142         Page previousPage;
143         bool isCrossPagePostBack;
144         bool isPostBack;
145         bool isCallback;
146         ArrayList requireStateControls;
147         Hashtable _validatorsByGroup;
148         HtmlForm _form;
149
150         string _title;
151         string _theme;
152         string _styleSheetTheme;
153         Hashtable items;
154
155         bool _maintainScrollPositionOnPostBack;
156
157         private bool asyncMode = false;
158         private TimeSpan asyncTimeout;
159         private const double DefaultAsyncTimeout = 45.0;
160         private List<PageAsyncTask> parallelTasks;
161         private List<PageAsyncTask> serialTasks;
162
163         private ViewStateEncryptionMode viewStateEncryptionMode;
164         private bool controlRegisteredForViewStateEncryption = false;
165 #endif
166
167         #region Constructor
168         public Page ()
169         {
170                 scriptManager = new ClientScriptManager (this);
171                 Page = this;
172                 ID = "__Page";
173 #if NET_2_0
174                 PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
175                 if (ps != null) {
176                         asyncTimeout = ps.AsyncTimeout;
177                         viewStateEncryptionMode = ps.ViewStateEncryptionMode;
178                 } else {
179                         asyncTimeout = TimeSpan.FromSeconds (DefaultAsyncTimeout);
180                         viewStateEncryptionMode = ViewStateEncryptionMode.Auto;
181                 }
182 #endif
183         }
184
185         #endregion              
186
187         #region Properties
188
189         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
190         [Browsable (false)]
191         public HttpApplicationState Application
192         {
193                 get {
194                         if (_context == null)
195                                 return null;
196                         return _context.Application;
197                 }
198         }
199
200         [EditorBrowsable (EditorBrowsableState.Never)]
201         protected bool AspCompatMode
202         {
203 #if NET_2_0
204                 get { return false; }
205 #endif
206                 set { throw new NotImplementedException (); }
207         }
208
209         [EditorBrowsable (EditorBrowsableState.Never)]
210 #if NET_2_0
211         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
212         [BrowsableAttribute (false)]
213         public bool Buffer
214         {
215                 get { return Response.BufferOutput; }
216                 set { Response.BufferOutput = value; }
217         }
218 #else
219         protected bool Buffer
220         {
221                 set { Response.BufferOutput = value; }
222         }
223 #endif
224
225         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
226         [Browsable (false)]
227         public Cache Cache
228         {
229                 get {
230                         if (_context == null)
231                                 throw new HttpException ("No cache available without a context.");
232                         return _context.Cache;
233                 }
234         }
235
236 #if NET_2_0
237         [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
238 #endif
239         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
240         [Browsable (false), DefaultValue ("")]
241         [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
242         public string ClientTarget
243         {
244                 get { return (clientTarget == null) ? "" : clientTarget; }
245                 set {
246                         clientTarget = value;
247                         if (value == "")
248                                 clientTarget = null;
249                 }
250         }
251
252         [EditorBrowsable (EditorBrowsableState.Never)]
253 #if NET_2_0
254         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
255         [BrowsableAttribute (false)]
256         public int CodePage
257         {
258                 get { return Response.ContentEncoding.CodePage; }
259                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
260         }
261 #else
262         protected int CodePage
263         {
264                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
265         }
266 #endif
267
268         [EditorBrowsable (EditorBrowsableState.Never)]
269 #if NET_2_0
270         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
271         [BrowsableAttribute (false)]
272         public string ContentType
273         {
274                 get { return Response.ContentType; }
275                 set { Response.ContentType = value; }
276         }
277 #else
278         protected string ContentType
279         {
280                 set { Response.ContentType = value; }
281         }
282 #endif
283
284         protected override HttpContext Context
285         {
286                 get {
287                         if (_context == null)
288                                 return HttpContext.Current;
289
290                         return _context;
291                 }
292         }
293
294 #if NET_2_0
295         [EditorBrowsable (EditorBrowsableState.Advanced)]
296         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
297         [BrowsableAttribute (false)]
298         public string Culture
299         {
300                 get { return Thread.CurrentThread.CurrentCulture.Name; }
301                 set { Thread.CurrentThread.CurrentCulture = GetPageCulture (value, Thread.CurrentThread.CurrentCulture); }
302         }
303 #else
304         [EditorBrowsable (EditorBrowsableState.Never)]
305         protected string Culture
306         {
307                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
308         }
309 #endif
310
311 #if NET_2_0
312         public virtual bool EnableEventValidation {
313                 get { return _eventValidation; }
314                 set {
315                         if (_lifeCycle > PageLifeCycle.Init)
316                                 throw new InvalidOperationException ("The 'EnableEventValidation' property can be set only in the Page_init, the Page directive or in the <pages> configuration section.");
317                         _eventValidation = value;
318                 }
319         }
320
321         internal PageLifeCycle LifeCycle {
322                 get { return _lifeCycle; }
323         }
324 #endif
325
326         [Browsable (false)]
327         public override bool EnableViewState
328         {
329                 get { return _viewState; }
330                 set { _viewState = value; }
331         }
332
333 #if NET_2_0
334         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
335         [BrowsableAttribute (false)]
336 #endif
337         [EditorBrowsable (EditorBrowsableState.Never)]
338 #if NET_2_0
339         public
340 #else
341         protected
342 #endif
343         bool EnableViewStateMac
344         {
345                 get { return _viewStateMac; }
346                 set { _viewStateMac = value; }
347         }
348
349 #if NET_1_1
350         internal bool EnableViewStateMacInternal {
351                 get { return _viewStateMac; }
352                 set { _viewStateMac = value; }
353         }
354 #endif
355         
356         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
357         [Browsable (false), DefaultValue ("")]
358         [WebSysDescription ("The URL of a page used for error redirection.")]
359         public string ErrorPage
360         {
361                 get { return _errorPage; }
362                 set {
363                         _errorPage = value;
364                         if (_context != null)
365                                 _context.ErrorPage = value;
366                 }
367         }
368
369 #if NET_2_0
370         [Obsolete]
371 #endif
372         [EditorBrowsable (EditorBrowsableState.Never)]
373         protected ArrayList FileDependencies
374         {
375                 set {
376                         if (Response != null)
377                                 Response.AddFileDependencies (value);
378                 }
379         }
380
381         [Browsable (false)]
382 #if NET_2_0
383         [EditorBrowsable (EditorBrowsableState.Never)]
384 #endif
385         public override string ID
386         {
387                 get { return base.ID; }
388                 set { base.ID = value; }
389         }
390
391         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
392         [Browsable (false)]
393         public bool IsPostBack
394         {
395                 get {
396 #if NET_2_0
397                         return isPostBack;
398 #else
399                         return _requestValueCollection != null;
400 #endif
401                 }
402         }
403
404         [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
405         public bool IsReusable {
406                 get { return false; }
407         }
408
409         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
410         [Browsable (false)]
411         public bool IsValid {
412                 get {
413                         if (!is_validated)
414                                 throw new HttpException (Locale.GetText ("Page hasn't been validated."));
415
416 #if NET_2_0
417                         foreach (IValidator val in Validators)
418                                 if (!val.IsValid)
419                                         return false;
420                         return true;
421 #else
422                         return ValidateCollection (_validators);
423 #endif
424                 }
425         }
426 #if NET_2_0
427         public IDictionary Items {
428                 get {
429                         if (items == null)
430                                 items = new Hashtable ();
431                         return items;
432                 }
433         }
434 #endif
435
436         [EditorBrowsable (EditorBrowsableState.Never)]
437 #if NET_2_0
438         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
439         [BrowsableAttribute (false)]
440         public int LCID {
441                 get { return Thread.CurrentThread.CurrentCulture.LCID; }
442                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
443         }
444 #else
445         protected int LCID {
446                 set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
447         }
448 #endif
449
450 #if NET_2_0
451         [Browsable (false)]
452         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
453         public bool MaintainScrollPositionOnPostBack {
454                 get { return _maintainScrollPositionOnPostBack; }
455                 set { _maintainScrollPositionOnPostBack = value; }
456         }
457 #endif
458
459 #if NET_2_0
460         public PageAdapter PageAdapter {
461                 get {
462                         return (PageAdapter)Adapter;
463                 }
464         }
465 #endif
466
467 #if !TARGET_J2EE
468         internal string theForm {
469                 get {
470                         return "theForm";
471                 }
472         }
473         
474         internal bool IsMultiForm {
475                 get { return false; }
476         }
477 #endif
478
479         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
480         [Browsable (false)]
481         public HttpRequest Request
482         {
483                 get {
484                         if (_context != null)
485                                 return _context.Request;
486
487                         throw new HttpException("Request is not available without context");
488                 }
489         }
490
491         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
492         [Browsable (false)]
493         public HttpResponse Response
494         {
495                 get {
496                         if (_context != null)
497                                 return _context.Response;
498
499                         throw new HttpException ("Response is not available without context");
500                 }
501         }
502
503         [EditorBrowsable (EditorBrowsableState.Never)]
504 #if NET_2_0
505         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
506         [BrowsableAttribute (false)]
507         public string ResponseEncoding
508         {
509                 get { return Response.ContentEncoding.WebName; }
510                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
511         }
512 #else
513         protected string ResponseEncoding
514         {
515                 set { Response.ContentEncoding = Encoding.GetEncoding (value); }
516         }
517 #endif
518
519         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
520         [Browsable (false)]
521         public HttpServerUtility Server
522         {
523                 get {
524                         return Context.Server;
525                 }
526         }
527
528         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
529         [Browsable (false)]
530         public virtual HttpSessionState Session
531         {
532                 get {
533                         if (_context == null)
534                                 _context = HttpContext.Current;
535
536                         if (_context == null)
537                                 throw new HttpException ("Session is not available without context");
538
539                         if (_context.Session == null)
540                                 throw new HttpException ("Session state can only be used " +
541                                                 "when enableSessionState is set to true, either " +
542                                                 "in a configuration file or in the Page directive.");
543
544                         return _context.Session;
545                 }
546         }
547
548 #if NET_2_0
549         [FilterableAttribute (false)]
550         [Obsolete]
551 #endif
552         [Browsable (false)]
553         public bool SmartNavigation
554         {
555                 get { return _smartNavigation; }
556                 set { _smartNavigation = value; }
557         }
558
559 #if NET_2_0
560         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
561         [Filterable (false)]
562         [Browsable (false)]
563         public virtual string StyleSheetTheme {
564                 get { return _styleSheetTheme; }
565                 set { _styleSheetTheme = value; }
566         }
567
568         [Browsable (false)]
569         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
570         public virtual string Theme {
571                 get { return _theme; }
572                 set { _theme = value; }
573         }
574
575         void InitializeStyleSheet ()
576         {
577                 if (_styleSheetTheme == null) {
578                         PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
579                         if (ps != null)
580                                 _styleSheetTheme = ps.StyleSheetTheme;
581                 }
582                 if (_styleSheetTheme != null && _styleSheetTheme != "")
583                         _styleSheetPageTheme = ThemeDirectoryCompiler.GetCompiledInstance (_styleSheetTheme, _context);
584         }
585
586         void InitializeTheme ()
587         {
588                 if (_theme == null) {
589                         PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
590                         if (ps != null)
591                                 _theme = ps.Theme;
592                 }
593                 if (_theme != null && _theme != "") {
594                         _pageTheme = ThemeDirectoryCompiler.GetCompiledInstance (_theme, _context);
595                         _pageTheme.SetPage (this);
596                 }
597         }
598
599 #endif
600
601 #if NET_2_0
602         [Localizable (true)] 
603         [Bindable (true)] 
604         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
605         public string Title {
606                 get {
607                         if (_title == null) {
608                                 if (htmlHeader != null)
609                                         return htmlHeader.Title;
610                                 return String.Empty;
611                         }
612                         return _title;
613                 }
614                 set {
615                         if (htmlHeader != null)
616                                 htmlHeader.Title = value;
617                         else
618                                 _title = value;
619                 }
620         }
621 #endif
622
623         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
624         [Browsable (false)]
625         public TraceContext Trace
626         {
627                 get { return Context.Trace; }
628         }
629
630         [EditorBrowsable (EditorBrowsableState.Never)]
631 #if NET_2_0
632         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
633         [BrowsableAttribute (false)]
634         public bool TraceEnabled
635         {
636                 get { return Trace.IsEnabled; }
637                 set { Trace.IsEnabled = value; }
638         }
639 #else
640         protected bool TraceEnabled
641         {
642                 set { Trace.IsEnabled = value; }
643         }
644 #endif
645
646         [EditorBrowsable (EditorBrowsableState.Never)]
647 #if NET_2_0
648         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
649         [BrowsableAttribute (false)]
650         public TraceMode TraceModeValue
651         {
652                 get { return Trace.TraceMode; }
653                 set { Trace.TraceMode = value; }
654         }
655 #else
656         protected TraceMode TraceModeValue
657         {
658                 set { Trace.TraceMode = value; }
659         }
660 #endif
661
662         [EditorBrowsable (EditorBrowsableState.Never)]
663         protected int TransactionMode
664         {
665 #if NET_2_0
666                 get { return _transactionMode; }
667 #endif
668                 set { _transactionMode = value; }
669         }
670
671 #if !NET_2_0
672         //
673         // This method is here just to remove the warning about "_transactionMode" not being
674         // used.  We probably will use it internally at some point.
675         //
676         internal int GetTransactionMode ()
677         {
678                 return _transactionMode;
679         }
680 #endif
681         
682 #if NET_2_0
683         [EditorBrowsable (EditorBrowsableState.Advanced)]
684         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
685         [BrowsableAttribute (false)]
686         public string UICulture
687         {
688                 get { return Thread.CurrentThread.CurrentUICulture.Name; }
689                 set { Thread.CurrentThread.CurrentUICulture = GetPageCulture (value, Thread.CurrentThread.CurrentUICulture); }
690         }
691 #else
692         [EditorBrowsable (EditorBrowsableState.Never)]
693         protected string UICulture
694         {
695                 set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
696         }
697 #endif
698
699         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
700         [Browsable (false)]
701         public IPrincipal User
702         {
703                 get { return Context.User; }
704         }
705
706         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
707         [Browsable (false)]
708         public ValidatorCollection Validators
709         {
710                 get { 
711                         if (_validators == null)
712                                 _validators = new ValidatorCollection ();
713                         return _validators;
714                 }
715         }
716
717         [MonoTODO ("Use this when encrypting/decrypting ViewState")]
718         [Browsable (false)]
719         public string ViewStateUserKey {
720                 get { return viewStateUserKey; }
721                 set { viewStateUserKey = value; }
722         }
723
724         [Browsable (false)]
725         public override bool Visible
726         {
727                 get { return base.Visible; }
728                 set { base.Visible = value; }
729         }
730
731         #endregion
732
733         #region Methods
734
735 #if NET_2_0
736         CultureInfo GetPageCulture (string culture, CultureInfo deflt)
737         {
738                 if (culture == null)
739                         return deflt;
740                 CultureInfo ret = null;
741                 if (culture.StartsWith ("auto", StringComparison.InvariantCultureIgnoreCase)) {
742 #if TARGET_J2EE
743                         if (Context.IsPortletRequest)
744                                 return deflt;
745 #endif
746                         string[] languages = Request.UserLanguages;
747                         try {
748                                 if (languages != null && languages.Length > 0)
749                                         ret = CultureInfo.CreateSpecificCulture (languages[0]);
750                         } catch {
751                         }
752                         
753                         if (ret == null)
754                                 ret = deflt;
755                 } else
756                         ret = CultureInfo.CreateSpecificCulture (culture);
757
758                 return ret;
759         }
760 #endif
761
762         [EditorBrowsable (EditorBrowsableState.Never)]
763         protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
764                                                              AsyncCallback cb, 
765                                                              object extraData)
766         {
767                 throw new NotImplementedException ();
768         }
769
770         [EditorBrowsable (EditorBrowsableState.Never)]
771         protected void AspCompatEndProcessRequest (IAsyncResult result)
772         {
773                 throw new NotImplementedException ();
774         }
775         
776         [EditorBrowsable (EditorBrowsableState.Advanced)]
777         protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
778         {
779                 return new HtmlTextWriter (tw);
780         }
781
782         [EditorBrowsable (EditorBrowsableState.Never)]
783         public void DesignerInitialize ()
784         {
785                 InitRecursive (null);
786         }
787
788         [EditorBrowsable (EditorBrowsableState.Advanced)]
789         protected virtual NameValueCollection DeterminePostBackMode ()
790         {
791                 if (_context == null)
792                         return null;
793
794                 HttpRequest req = _context.Request;
795                 if (req == null)
796                         return null;
797
798                 NameValueCollection coll = null;
799                 if (0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture))
800                         coll = req.Form;
801 #if TARGET_J2EE
802                 else if (IsPortletRender && req.Form ["__VIEWSTATE"] != null)
803                         coll = req.Form;
804 #endif
805                 else
806                         coll = req.QueryString;
807
808                 WebROCollection c = (WebROCollection) coll;
809                 allow_load = !c.GotID;
810                 if (allow_load)
811                         c.ID = GetTypeHashCode ();
812                 else
813                         allow_load = (c.ID == GetTypeHashCode ());
814
815                 if (coll != null && coll ["__VIEWSTATE"] == null && coll ["__EVENTTARGET"] == null)
816                         return null;
817
818                 return coll;
819         }
820
821 #if NET_2_0
822         public override Control FindControl (string id) {
823                 if (id == ID)
824                         return this;
825                 else
826                         return base.FindControl (id);
827         }
828 #endif
829
830 #if NET_2_0
831         [Obsolete]
832 #endif
833         [EditorBrowsable (EditorBrowsableState.Advanced)]
834         public string GetPostBackClientEvent (Control control, string argument)
835         {
836                 return scriptManager.GetPostBackEventReference (control, argument);
837         }
838
839 #if NET_2_0
840         [Obsolete]
841 #endif
842         [EditorBrowsable (EditorBrowsableState.Advanced)]
843         public string GetPostBackClientHyperlink (Control control, string argument)
844         {
845                 return scriptManager.GetPostBackClientHyperlink (control, argument);
846         }
847
848 #if NET_2_0
849         [Obsolete]
850 #endif
851         [EditorBrowsable (EditorBrowsableState.Advanced)]
852         public string GetPostBackEventReference (Control control)
853         {
854                 return scriptManager.GetPostBackEventReference (control, "");
855         }
856
857 #if NET_2_0
858         [Obsolete]
859 #endif
860         [EditorBrowsable (EditorBrowsableState.Advanced)]
861         public string GetPostBackEventReference (Control control, string argument)
862         {
863                 return scriptManager.GetPostBackEventReference (control, argument);
864         }
865
866         internal void RequiresPostBackScript ()
867         {
868 #if NET_2_0
869                 if (requiresPostBackScript)
870                         return;
871                 ClientScript.RegisterHiddenField (postEventSourceID, String.Empty);
872                 ClientScript.RegisterHiddenField (postEventArgumentID, String.Empty);
873 #endif
874                 requiresPostBackScript = true;
875         }
876
877         [EditorBrowsable (EditorBrowsableState.Never)]
878         public virtual int GetTypeHashCode ()
879         {
880                 return 0;
881         }
882
883 #if NET_2_0
884     [MonoTODO("The following properties of OutputCacheParameters are silently ignored: CacheProfile, NoStore, SqlDependency")]
885     protected internal virtual void InitOutputCache(OutputCacheParameters cacheSettings)
886     {
887         if (cacheSettings.Enabled)
888             InitOutputCache(cacheSettings.Duration,
889                 cacheSettings.VaryByHeader,
890                 cacheSettings.VaryByCustom,
891                 cacheSettings.Location,
892                 cacheSettings.VaryByParam);
893     }
894 #endif
895
896         [EditorBrowsable (EditorBrowsableState.Never)]
897         protected virtual void InitOutputCache (int duration,
898                                                 string varyByHeader,
899                                                 string varyByCustom,
900                                                 OutputCacheLocation location,
901                                                 string varyByParam)
902         {
903                 HttpCachePolicy cache = _context.Response.Cache;
904                 bool set_vary = false;
905
906                 switch (location) {
907                 case OutputCacheLocation.Any:
908                         cache.SetCacheability (HttpCacheability.Public);
909                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
910                         cache.SetLastModified (_context.Timestamp);
911                         set_vary = true;
912                         break;
913                 case OutputCacheLocation.Client:
914                         cache.SetCacheability (HttpCacheability.Private);
915                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
916                         cache.SetLastModified (_context.Timestamp);
917                         break;
918                 case OutputCacheLocation.Downstream:
919                         cache.SetCacheability (HttpCacheability.Public);
920                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
921                         cache.SetLastModified (_context.Timestamp);
922                         break;
923                 case OutputCacheLocation.Server:                        
924                         cache.SetCacheability (HttpCacheability.Server);
925                         set_vary = true;
926                         break;
927                 case OutputCacheLocation.None:
928                         break;
929                 }
930
931                 if (set_vary) {
932                         if (varyByCustom != null)
933                                 cache.SetVaryByCustom (varyByCustom);
934
935                         if (varyByParam != null && varyByParam.Length > 0) {
936                                 string[] prms = varyByParam.Split (';');
937                                 foreach (string p in prms)
938                                         cache.VaryByParams [p.Trim ()] = true;
939                                 cache.VaryByParams.IgnoreParams = false;
940                         } else {
941                                 cache.VaryByParams.IgnoreParams = true;
942                         }
943                         
944                         if (varyByHeader != null && varyByHeader.Length > 0) {
945                                 string[] hdrs = varyByHeader.Split (';');
946                                 foreach (string h in hdrs)
947                                         cache.VaryByHeaders [h.Trim ()] = true;
948                         }
949                 }
950                         
951                 cache.Duration = duration;
952                 cache.SetExpires (_context.Timestamp.AddSeconds (duration));
953         }
954
955 #if NET_2_0
956         [Obsolete]
957 #else
958         [EditorBrowsable (EditorBrowsableState.Advanced)]
959 #endif
960         public bool IsClientScriptBlockRegistered (string key)
961         {
962                 return scriptManager.IsClientScriptBlockRegistered (key);
963         }
964
965 #if NET_2_0
966         [Obsolete]
967 #else
968         [EditorBrowsable (EditorBrowsableState.Advanced)]
969 #endif
970         public bool IsStartupScriptRegistered (string key)
971         {
972                 return scriptManager.IsStartupScriptRegistered (key);
973         }
974
975         public string MapPath (string virtualPath)
976         {
977                 return Request.MapPath (virtualPath);
978         }
979
980 #if NET_2_0
981         protected internal override void Render (HtmlTextWriter writer) {
982                 if (MaintainScrollPositionOnPostBack) {
983                         ClientScript.RegisterWebFormClientScript ();
984
985                         ClientScript.RegisterHiddenField (ScrollPositionXID, Request [ScrollPositionXID]);
986                         ClientScript.RegisterHiddenField (ScrollPositionYID, Request [ScrollPositionYID]);
987                         
988                         StringBuilder script = new StringBuilder ();
989                         script.AppendLine ("<script type=\"text/javascript\">");
990                         script.AppendLine ("<!--");
991                         script.AppendLine (theForm + ".oldSubmit = " + theForm + ".submit;");
992                         script.AppendLine (theForm + ".submit = WebForm_SaveScrollPositionSubmit;");
993                         script.AppendLine (theForm + ".oldOnSubmit = " + theForm + ".onsubmit;");
994                         script.AppendLine (theForm + ".onsubmit = WebForm_SaveScrollPositionOnSubmit;");
995                         if (IsPostBack) {
996                                 script.AppendLine (theForm + ".oldOnLoad = window.onload;");
997                                 script.AppendLine ("window.onload = function () { WebForm_RestoreScrollPosition (" + theForm + "); };");
998                         }
999                         script.AppendLine ("// -->");
1000                         script.AppendLine ("</script>");
1001                         
1002                         ClientScript.RegisterStartupScript (typeof (Page), "MaintainScrollPositionOnPostBackStartup", script.ToString());
1003                 }
1004                 scriptManager.ResetEventValidationState ();
1005                 base.Render (writer);
1006         }
1007 #endif
1008
1009         private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
1010         {
1011 #if ONLY_1_1
1012                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
1013                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
1014 #endif
1015                 writer.WriteLine ();
1016                 
1017                 ClientScriptManager.WriteBeginScriptBlock (writer);
1018
1019 #if ONLY_1_1
1020                 RenderClientScriptFormDeclaration (writer, formUniqueID);
1021 #endif
1022                 if (IsMultiForm) {
1023                         writer.WriteLine ("{0}._form = {0};", theForm);
1024                         writer.Write (theForm + ".");
1025                 }
1026                 else {
1027                         writer.WriteLine ("window._form = {0};", theForm);
1028                 }
1029                 writer.WriteLine ("__doPostBack = function (eventTarget, eventArgument) {");
1030 #if NET_2_0
1031                 writer.WriteLine ("\tif(this._form.onsubmit && this._form.onsubmit() == false) return;");
1032 #else
1033                 writer.WriteLine ("\tif(document.ValidatorOnSubmit && !ValidatorOnSubmit()) return;");
1034 #endif
1035                 writer.WriteLine ("\tthis._form.{0}.value = eventTarget;", postEventSourceID);
1036                 writer.WriteLine ("\tthis._form.{0}.value = eventArgument;", postEventArgumentID);
1037                 writer.WriteLine ("\tthis._form.submit();");
1038                 writer.WriteLine ("}");
1039                 ClientScriptManager.WriteEndScriptBlock (writer);
1040         }
1041
1042         void RenderClientScriptFormDeclaration (HtmlTextWriter writer, string formUniqueID)
1043         {
1044                 writer.WriteLine ("\tvar {0};\n\tif (document.getElementById) {{ {0} = document.getElementById ('{1}'); }}", theForm, formUniqueID);
1045                 writer.WriteLine ("\telse {{ {0} = document.{1}; }}", theForm, formUniqueID);
1046                 writer.WriteLine ("\t{0}._instanceVariableName = '{0}';", theForm);
1047 #if TARGET_J2EE
1048                 string serverUrl = Context.ServletResponse.encodeURL (Request.RawUrl);
1049                 writer.WriteLine ("\t{0}.serverURL = {1};", theForm, ClientScriptManager.GetScriptLiteral (serverUrl));
1050                 writer.WriteLine ("\twindow.TARGET_J2EE = true;");
1051                 writer.WriteLine ("\twindow.IsMultiForm = {0};", IsMultiForm ? "true" : "false");
1052 #endif
1053         }
1054
1055         internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
1056         {
1057                 if (renderingForm)
1058                         throw new HttpException ("Only 1 HtmlForm is allowed per page.");
1059
1060                 renderingForm = true;
1061                 writer.WriteLine ();
1062
1063 #if NET_2_0
1064                 ClientScriptManager.WriteBeginScriptBlock (writer);
1065                 RenderClientScriptFormDeclaration (writer, formUniqueID);
1066                 ClientScriptManager.WriteEndScriptBlock (writer);
1067 #endif
1068
1069                 if (handleViewState)
1070                         scriptManager.RegisterHiddenField ("__VIEWSTATE", _savedViewState);
1071
1072                 scriptManager.WriteHiddenFields (writer);
1073                 if (requiresPostBackScript) {
1074                         RenderPostBackScript (writer, formUniqueID);
1075                         postBackScriptRendered = true;
1076                 }
1077 #if NET_2_0
1078                 scriptManager.WriteWebFormClientScript (writer);
1079 #endif
1080                 scriptManager.WriteClientScriptBlocks (writer);
1081         }
1082
1083         internal IStateFormatter GetFormatter ()
1084         {
1085                 return new ObjectStateFormatter (this);
1086         }
1087
1088         internal string GetSavedViewState ()
1089         {
1090                 return _savedViewState;
1091         }
1092
1093         internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
1094         {
1095 #if NET_2_0
1096                 scriptManager.SaveEventValidationState ();
1097                 scriptManager.WriteExpandoAttributes (writer);
1098 #endif
1099                 scriptManager.WriteHiddenFields (writer);
1100                 if (!postBackScriptRendered && requiresPostBackScript)
1101                         RenderPostBackScript (writer, formUniqueID);
1102 #if NET_2_0
1103                 scriptManager.WriteWebFormClientScript (writer);
1104 #endif
1105
1106                 scriptManager.WriteArrayDeclares (writer);
1107                 scriptManager.WriteStartupScriptBlocks (writer);
1108                 renderingForm = false;
1109                 postBackScriptRendered = false;
1110         }
1111
1112         private void ProcessPostData (NameValueCollection data, bool second)
1113         {
1114                 if (data != null) {
1115                         Hashtable used = new Hashtable ();
1116                         foreach (string id in data.AllKeys){
1117                                 if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
1118                                         continue;
1119
1120                                 string real_id = id;
1121                                 int dot = real_id.IndexOf ('.');
1122                                 if (dot >= 1)
1123                                         real_id = real_id.Substring (0, dot);
1124                         
1125                                 if (real_id == null || used.ContainsKey (real_id))
1126                                         continue;
1127
1128                                 used.Add (real_id, real_id);
1129
1130                                 Control ctrl = FindControl (real_id);
1131                                 if (ctrl != null){
1132                                         IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
1133                                         IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
1134
1135                                         if (pbdh == null) {
1136                                                 if (pbeh != null)
1137                                                         RegisterRequiresRaiseEvent (pbeh);
1138                                                 continue;
1139                                         }
1140                 
1141                                         if (pbdh.LoadPostData (real_id, data) == true) {
1142                                                 if (requiresPostDataChanged == null)
1143                                                         requiresPostDataChanged = new ArrayList ();
1144                                                 requiresPostDataChanged.Add (pbdh);
1145                                         }
1146                                 
1147                                         if (_requiresPostBackCopy != null)
1148                                                 _requiresPostBackCopy.Remove (real_id);
1149
1150                                 } else if (!second) {
1151                                         if (secondPostData == null)
1152                                                 secondPostData = new NameValueCollection ();
1153                                         secondPostData.Add (real_id, data [id]);
1154                                 }
1155                         }
1156                 }
1157
1158                 ArrayList list1 = null;
1159                 if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
1160                         string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
1161                         foreach (string id in handlers) {
1162                                 IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
1163                                 if (pbdh != null) {                     
1164                                         _requiresPostBackCopy.Remove (id);
1165                                         if (pbdh.LoadPostData (id, data)) {
1166                                                 if (requiresPostDataChanged == null)
1167                                                         requiresPostDataChanged = new ArrayList ();
1168         
1169                                                 requiresPostDataChanged.Add (pbdh);
1170                                         }
1171                                 } else if (!second) {
1172                                         if (list1 == null)
1173                                                 list1 = new ArrayList ();
1174                                         list1.Add (id);
1175                                 }
1176                         }
1177                 }
1178                 _requiresPostBackCopy = second ? null : list1;
1179                 if (second)
1180                         secondPostData = null;
1181         }
1182
1183         [EditorBrowsable (EditorBrowsableState.Never)]
1184 #if NET_2_0
1185         public virtual void ProcessRequest (HttpContext context)
1186 #else
1187         public void ProcessRequest (HttpContext context)
1188 #endif
1189         {
1190 #if NET_2_0
1191                 _lifeCycle = PageLifeCycle.Unknown;
1192 #endif
1193                 _context = context;
1194                 if (clientTarget != null)
1195                         Request.ClientTarget = clientTarget;
1196
1197                 WireupAutomaticEvents ();
1198                 //-- Control execution lifecycle in the docs
1199
1200                 // Save culture information because it can be modified in FrameworkInitialize()
1201                 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
1202                 CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
1203                 FrameworkInitialize ();
1204                 context.ErrorPage = _errorPage;
1205
1206                 try {
1207                         InternalProcessRequest ();
1208                 } catch (ThreadAbortException) {
1209                         // Do nothing, just ignore it by now.
1210                 } catch (Exception e) {
1211                         context.AddError (e); // OnError might access LastError
1212                         OnError (EventArgs.Empty);
1213                         context.ClearError (e);
1214                         // We want to remove that error, as we're rethrowing to stop
1215                         // further processing.
1216                         Trace.Warn ("Unhandled Exception", e.ToString (), e);
1217                         throw;
1218                 } finally {
1219                         try {
1220 #if NET_2_0
1221                                 _lifeCycle = PageLifeCycle.Unload;
1222 #endif
1223                                 RenderTrace ();
1224                                 UnloadRecursive (true);
1225 #if NET_2_0
1226                                 _lifeCycle = PageLifeCycle.End;
1227 #endif
1228                         } catch {}
1229                         if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
1230                                 Thread.CurrentThread.CurrentCulture = culture;
1231
1232                         if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
1233                                 Thread.CurrentThread.CurrentUICulture = uiculture;
1234                 }
1235         }
1236         
1237 #if NET_2_0
1238         delegate void ProcessRequestDelegate (HttpContext context);
1239
1240         private sealed class DummyAsyncResult : IAsyncResult
1241         {
1242                 readonly object state;
1243                 readonly WaitHandle asyncWaitHandle;
1244                 readonly bool completedSynchronously;
1245                 readonly bool isCompleted;
1246
1247                 public DummyAsyncResult (bool isCompleted, bool completedSynchronously, object state) 
1248                 {
1249                         this.isCompleted = isCompleted;
1250                         this.completedSynchronously = completedSynchronously;
1251                         this.state = state;
1252                         if (isCompleted) {
1253                                 asyncWaitHandle = new ManualResetEvent (true);
1254                         }
1255                         else {
1256                                 asyncWaitHandle = new ManualResetEvent (false);
1257                         }
1258                 }
1259
1260                 #region IAsyncResult Members
1261
1262                 public object AsyncState {
1263                         get { return state; }
1264                 }
1265
1266                 public WaitHandle AsyncWaitHandle {
1267                         get { return asyncWaitHandle; }
1268                 }
1269
1270                 public bool CompletedSynchronously {
1271                         get { return completedSynchronously; }
1272                 }
1273
1274                 public bool IsCompleted {
1275                         get { return isCompleted; }
1276                 }
1277
1278                 #endregion
1279         }
1280
1281         protected IAsyncResult AsyncPageBeginProcessRequest (HttpContext context, AsyncCallback callback, object extraData) 
1282         {
1283                 ProcessRequest (context);
1284                 DummyAsyncResult asyncResult = new DummyAsyncResult (true, true, extraData);
1285
1286                 if (callback != null) {
1287                         callback (asyncResult);
1288                 }
1289                 
1290                 return asyncResult;
1291         }
1292
1293         protected void AsyncPageEndProcessRequest (IAsyncResult result) 
1294         {
1295         }
1296
1297         internal void ProcessCrossPagePostBack (HttpContext context)
1298         {
1299                 isCrossPagePostBack = true;
1300                 ProcessRequest (context);
1301         }
1302 #endif
1303
1304         void InternalProcessRequest ()
1305         {
1306                 _requestValueCollection = this.DeterminePostBackMode();
1307
1308 #if NET_2_0
1309                 _lifeCycle = PageLifeCycle.Start;
1310                 // http://msdn2.microsoft.com/en-us/library/ms178141.aspx
1311                 if (_requestValueCollection != null) {
1312                         if (!isCrossPagePostBack && _requestValueCollection [PreviousPageID] != null && _requestValueCollection [PreviousPageID] != Request.FilePath) {
1313                                 _doLoadPreviousPage = true;
1314                         }
1315                         else {
1316                                 isCallback = _requestValueCollection [CallbackArgumentID] != null;
1317                                 // LAMESPEC: on Callback IsPostBack is set to false, but true.
1318                                 //isPostBack = !isCallback;
1319                                 isPostBack = true;
1320                         }
1321                         string lastFocus = _requestValueCollection [LastFocusID];
1322                         if (!String.IsNullOrEmpty (lastFocus)) {
1323                                 _focusedControlID = UniqueID2ClientID (lastFocus);
1324                         }
1325                 }
1326                 
1327                 // if request was transfered from other page - track Prev. Page
1328                 previousPage = _context.LastPage;
1329                 _context.LastPage = this;
1330
1331                 _lifeCycle = PageLifeCycle.PreInit;
1332                 OnPreInit (EventArgs.Empty);
1333
1334                 InitializeTheme ();
1335                 ApplyMasterPage ();
1336                 _lifeCycle = PageLifeCycle.Init;
1337 #endif
1338                 Trace.Write ("aspx.page", "Begin Init");
1339                 InitRecursive (null);
1340                 Trace.Write ("aspx.page", "End Init");
1341
1342 #if NET_2_0
1343                 _lifeCycle = PageLifeCycle.InitComplete;
1344                 OnInitComplete (EventArgs.Empty);
1345 #endif
1346                         
1347                 renderingForm = false;  
1348 #if NET_2_0
1349                 if (IsPostBack || IsCallback) {
1350                         _lifeCycle = PageLifeCycle.PreLoad;
1351                         if (_requestValueCollection != null)
1352                                 scriptManager.RestoreEventValidationState (_requestValueCollection [scriptManager.EventStateFieldName]);
1353 #else
1354                 if (IsPostBack) {
1355 #endif
1356                         Trace.Write ("aspx.page", "Begin LoadViewState");
1357                         LoadPageViewState ();
1358                         Trace.Write ("aspx.page", "End LoadViewState");
1359                         Trace.Write ("aspx.page", "Begin ProcessPostData");
1360 #if TARGET_J2EE
1361                         if (!IsGetBack)
1362 #endif
1363                         ProcessPostData (_requestValueCollection, false);
1364                         Trace.Write ("aspx.page", "End ProcessPostData");
1365                 }
1366
1367 #if NET_2_0
1368                 OnPreLoad (EventArgs.Empty);
1369                 _lifeCycle = PageLifeCycle.Load;
1370 #endif
1371
1372                 LoadRecursive ();
1373 #if NET_2_0
1374 #if TARGET_J2EE
1375                 if (!IsGetBack)
1376 #endif
1377                 if (IsPostBack || IsCallback) {
1378                         _lifeCycle = PageLifeCycle.ControlEvents;
1379 #else
1380                 if (IsPostBack) {
1381 #endif
1382                         Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
1383                         ProcessPostData (secondPostData, true);
1384                         Trace.Write ("aspx.page", "End ProcessPostData Second Try");
1385                         Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
1386                         RaiseChangedEvents ();
1387                         Trace.Write ("aspx.page", "End Raise ChangedEvents");
1388                         Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
1389                         RaisePostBackEvents ();
1390                         Trace.Write ("aspx.page", "End Raise PostBackEvent");
1391                 }
1392                 
1393 #if NET_2_0
1394                 _lifeCycle = PageLifeCycle.LoadComplete;
1395                 OnLoadComplete (EventArgs.Empty);
1396
1397                 if (IsCrossPagePostBack)
1398                         return;
1399
1400                 if (IsCallback) {
1401                         string result = ProcessCallbackData ();
1402                         HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
1403                         callbackOutput.Write (result);
1404                         callbackOutput.Flush ();
1405                         return;
1406                 }
1407
1408                 _lifeCycle = PageLifeCycle.PreRender;
1409 #endif
1410                 
1411                 Trace.Write ("aspx.page", "Begin PreRender");
1412                 PreRenderRecursiveInternal ();
1413                 Trace.Write ("aspx.page", "End PreRender");
1414                 
1415 #if NET_2_0
1416                 ExecuteRegisteredAsyncTasks ();
1417
1418                 _lifeCycle = PageLifeCycle.PreRenderComplete;
1419                 OnPreRenderComplete (EventArgs.Empty);
1420 #endif
1421
1422                 Trace.Write ("aspx.page", "Begin SaveViewState");
1423                 SavePageViewState ();
1424                 Trace.Write ("aspx.page", "End SaveViewState");
1425                 
1426 #if NET_2_0
1427                 _lifeCycle = PageLifeCycle.SaveStateComplete;
1428                 OnSaveStateComplete (EventArgs.Empty);
1429 #if TARGET_J2EE
1430                 if (OnSaveStateCompleteForPortlet ())
1431                         return;
1432 #endif // TARGET_J2EE
1433 #endif // NET_2_0
1434
1435 #if NET_2_0
1436                 _lifeCycle = PageLifeCycle.Render;
1437 #endif
1438                 
1439                 //--
1440                 Trace.Write ("aspx.page", "Begin Render");
1441                 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1442                 RenderControl (output);
1443                 Trace.Write ("aspx.page", "End Render");
1444         }
1445
1446         private void RenderTrace ()
1447         {
1448                 TraceManager traceManager = HttpRuntime.TraceManager;
1449
1450                 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
1451                         return;
1452                 
1453                 Trace.SaveData ();
1454
1455                 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput) 
1456                         return;
1457
1458                 if (!traceManager.LocalOnly || Context.Request.IsLocal) {
1459                         HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1460                         Trace.Render (output);
1461                 }
1462         }
1463         
1464 #if NET_2_0
1465         bool CheckForValidationSupport (Control targetControl)
1466         {
1467                 if (targetControl == null)
1468                         return false;
1469                 Type type = targetControl.GetType ();
1470                 object[] attributes = type.GetCustomAttributes (false);
1471                 foreach (object attr in attributes)
1472                         if (attr is SupportsEventValidationAttribute)
1473                                 return true;
1474                 return false;
1475         }
1476 #endif
1477         
1478         void RaisePostBackEvents ()
1479         {
1480 #if NET_2_0
1481                 Control targetControl;
1482 #endif
1483                 if (requiresRaiseEvent != null) {
1484                         RaisePostBackEvent (requiresRaiseEvent, null);
1485                         return;
1486                 }
1487
1488                 NameValueCollection postdata = _requestValueCollection;
1489                 if (postdata == null)
1490                         return;
1491
1492                 string eventTarget = postdata [postEventSourceID];
1493                 if (eventTarget == null || eventTarget.Length == 0) {
1494                         Validate ();
1495                         return;
1496                 }
1497
1498 #if NET_2_0
1499                 targetControl = FindControl (eventTarget);
1500                 IPostBackEventHandler target = targetControl as IPostBackEventHandler;
1501 #else
1502                 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
1503 #endif
1504                         
1505                 if (target == null)
1506                         return;
1507
1508                 string eventArgument = postdata [postEventArgumentID];
1509                 RaisePostBackEvent (target, eventArgument);
1510         }
1511
1512         internal void RaiseChangedEvents ()
1513         {
1514                 if (requiresPostDataChanged == null)
1515                         return;
1516
1517                 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
1518                         ipdh.RaisePostDataChangedEvent ();
1519
1520                 requiresPostDataChanged = null;
1521         }
1522
1523         [EditorBrowsable (EditorBrowsableState.Advanced)]
1524         protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
1525         {
1526 #if NET_2_0
1527                 Control targetControl = sourceControl as Control;
1528                 if (targetControl != null && CheckForValidationSupport (targetControl))
1529                         scriptManager.ValidateEvent (targetControl.UniqueID, eventArgument);
1530 #endif
1531                 sourceControl.RaisePostBackEvent (eventArgument);
1532         }
1533         
1534 #if NET_2_0
1535         [Obsolete]
1536 #endif
1537         [EditorBrowsable (EditorBrowsableState.Advanced)]
1538         public void RegisterArrayDeclaration (string arrayName, string arrayValue)
1539         {
1540                 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
1541         }
1542
1543 #if NET_2_0
1544         [Obsolete]
1545 #endif
1546         [EditorBrowsable (EditorBrowsableState.Advanced)]
1547         public virtual void RegisterClientScriptBlock (string key, string script)
1548         {
1549                 scriptManager.RegisterClientScriptBlock (key, script);
1550         }
1551
1552 #if NET_2_0
1553         [Obsolete]
1554 #endif
1555         [EditorBrowsable (EditorBrowsableState.Advanced)]
1556         public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
1557         {
1558                 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
1559         }
1560
1561         [MonoTODO("Not implemented, Used in HtmlForm")]
1562         internal void RegisterClientScriptFile (string a, string b, string c)
1563         {
1564                 throw new NotImplementedException ();
1565         }
1566
1567 #if NET_2_0
1568         [Obsolete]
1569 #endif
1570         [EditorBrowsable (EditorBrowsableState.Advanced)]
1571         public void RegisterOnSubmitStatement (string key, string script)
1572         {
1573                 scriptManager.RegisterOnSubmitStatement (key, script);
1574         }
1575
1576         internal string GetSubmitStatements ()
1577         {
1578                 return scriptManager.WriteSubmitStatements ();
1579         }
1580
1581         [EditorBrowsable (EditorBrowsableState.Advanced)]
1582         public void RegisterRequiresPostBack (Control control)
1583         {
1584 #if NET_2_0
1585                 if (!(control is IPostBackDataHandler))
1586                         throw new HttpException ("The control to register does not implement the IPostBackDataHandler interface.");
1587 #endif
1588                 
1589                 if (_requiresPostBack == null)
1590                         _requiresPostBack = new ArrayList ();
1591
1592                 if (_requiresPostBack.Contains (control.UniqueID))
1593                         return;
1594
1595                 _requiresPostBack.Add (control.UniqueID);
1596         }
1597
1598         [EditorBrowsable (EditorBrowsableState.Advanced)]
1599         public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
1600         {
1601                 requiresRaiseEvent = control;
1602         }
1603
1604 #if NET_2_0
1605         [Obsolete]
1606 #endif
1607         [EditorBrowsable (EditorBrowsableState.Advanced)]
1608         public virtual void RegisterStartupScript (string key, string script)
1609         {
1610                 scriptManager.RegisterStartupScript (key, script);
1611         }
1612
1613         [EditorBrowsable (EditorBrowsableState.Advanced)]
1614         public void RegisterViewStateHandler ()
1615         {
1616                 handleViewState = true;
1617         }
1618
1619         [EditorBrowsable (EditorBrowsableState.Advanced)]
1620         protected virtual void SavePageStateToPersistenceMedium (object viewState)
1621         {
1622                 PageStatePersister persister = this.PageStatePersister;
1623                 if (persister == null)
1624                         return;
1625                 Pair pair = viewState as Pair;
1626                 if (pair != null) {
1627                         persister.ViewState = pair.First;
1628                         persister.ControlState = pair.Second;
1629                 } else
1630                         persister.ViewState = viewState;
1631                 persister.Save ();
1632         }
1633
1634         internal string RawViewState {
1635                 get {
1636                         NameValueCollection postdata = _requestValueCollection;
1637                         string view_state;
1638                         if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
1639                                 return null;
1640
1641                         if (view_state == "")
1642                                 return null;
1643                         return view_state;
1644                 }
1645                 set { _savedViewState = value; }
1646         }
1647
1648 #if NET_2_0
1649         protected virtual 
1650 #else
1651         internal
1652 #endif
1653         PageStatePersister PageStatePersister {
1654                 get {
1655                         if (page_state_persister == null)
1656                                 page_state_persister = new HiddenFieldPageStatePersister (this);
1657                         return page_state_persister;
1658                 }
1659         }
1660         
1661         [EditorBrowsable (EditorBrowsableState.Advanced)]
1662         protected virtual object LoadPageStateFromPersistenceMedium ()
1663         {
1664                 PageStatePersister persister = this.PageStatePersister;
1665                 if (persister == null)
1666                         return null;
1667                 persister.Load ();
1668                 return new Pair (persister.ViewState, persister.ControlState);
1669         }
1670
1671         internal void LoadPageViewState()
1672         {
1673                 Pair sState = LoadPageStateFromPersistenceMedium () as Pair;
1674                 if (sState != null) {
1675                         if (allow_load) {
1676 #if NET_2_0
1677                                 LoadPageControlState (sState.Second);
1678 #endif
1679                                 Pair vsr = sState.First as Pair;
1680                                 if (vsr != null) {
1681                                         LoadViewStateRecursive (vsr.First);
1682                                         _requiresPostBackCopy = vsr.Second as ArrayList;
1683                                 }
1684                         }
1685                 }
1686         }
1687
1688         internal void SavePageViewState ()
1689         {
1690                 if (!handleViewState)
1691                         return;
1692
1693 #if NET_2_0
1694                 object controlState = SavePageControlState ();
1695 #endif
1696
1697                 object viewState = SaveViewStateRecursive ();
1698                 object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
1699                 Pair vsr = null;
1700
1701                 if (viewState != null || reqPostback != null)
1702                         vsr = new Pair (viewState, reqPostback);
1703                 Pair pair = new Pair ();
1704
1705                 pair.First = vsr;
1706 #if NET_2_0
1707                 pair.Second = controlState;
1708 #else
1709                 pair.Second = null;
1710 #endif
1711                 if (pair.First == null && pair.Second == null)
1712                         SavePageStateToPersistenceMedium (null);
1713                 else
1714                         SavePageStateToPersistenceMedium (pair);                
1715
1716         }
1717
1718         public virtual void Validate ()
1719         {
1720                 is_validated = true;
1721                 ValidateCollection (_validators);
1722         }
1723
1724 #if NET_2_0
1725         internal bool AreValidatorsUplevel () {
1726                 return AreValidatorsUplevel (String.Empty);
1727         }
1728
1729         internal bool AreValidatorsUplevel (string valGroup)
1730 #else
1731         internal virtual bool AreValidatorsUplevel ()
1732 #endif
1733         {
1734                 bool uplevel = false;
1735
1736                 foreach (IValidator v in Validators) {
1737                         BaseValidator bv = v as BaseValidator;
1738                         if (bv == null) continue;
1739
1740 #if NET_2_0
1741                         if (valGroup != bv.ValidationGroup)
1742                                 continue;
1743 #endif
1744                         if (bv.GetRenderUplevel()) {
1745                                 uplevel = true;
1746                                 break;
1747                         }
1748                 }
1749
1750                 return uplevel;
1751         }
1752
1753         bool ValidateCollection (ValidatorCollection validators)
1754         {
1755 #if NET_2_0
1756                 if (!_eventValidation)
1757                         return true;
1758 #endif
1759
1760                 if (validators == null || validators.Count == 0)
1761                         return true;
1762
1763                 bool all_valid = true;
1764                 foreach (IValidator v in validators){
1765                         v.Validate ();
1766                         if (v.IsValid == false)
1767                                 all_valid = false;
1768                 }
1769
1770                 return all_valid;
1771         }
1772
1773         [EditorBrowsable (EditorBrowsableState.Advanced)]
1774         public virtual void VerifyRenderingInServerForm (Control control)
1775         {
1776                 if (_context == null)
1777                         return;
1778 #if NET_2_0
1779                 if (IsCallback)
1780                         return;
1781 #endif
1782                 if (!renderingForm)
1783                         throw new HttpException ("Control '" +
1784                                                  control.ClientID +
1785                                                  "' of type '" +
1786                                                  control.GetType ().Name +
1787                                                  "' must be placed inside a form tag with runat=server.");
1788         }
1789
1790         protected override void FrameworkInitialize ()
1791         {
1792                 base.FrameworkInitialize ();
1793 #if NET_2_0
1794                 InitializeStyleSheet ();
1795 #endif
1796         }
1797
1798         #endregion
1799         
1800         #if NET_2_0
1801         public
1802         #else
1803         internal
1804         #endif
1805                 ClientScriptManager ClientScript {
1806                 get { return scriptManager; }
1807         }
1808         
1809         #if NET_2_0
1810         
1811         static readonly object InitCompleteEvent = new object ();
1812         static readonly object LoadCompleteEvent = new object ();
1813         static readonly object PreInitEvent = new object ();
1814         static readonly object PreLoadEvent = new object ();
1815         static readonly object PreRenderCompleteEvent = new object ();
1816         static readonly object SaveStateCompleteEvent = new object ();
1817         int event_mask;
1818         const int initcomplete_mask = 1;
1819         const int loadcomplete_mask = 1 << 1;
1820         const int preinit_mask = 1 << 2;
1821         const int preload_mask = 1 << 3;
1822         const int prerendercomplete_mask = 1 << 4;
1823         const int savestatecomplete_mask = 1 << 5;
1824         
1825         [EditorBrowsable (EditorBrowsableState.Advanced)]
1826         public event EventHandler InitComplete {
1827                 add {
1828                         event_mask |= initcomplete_mask;
1829                         Events.AddHandler (InitCompleteEvent, value);
1830                 }
1831                 remove { Events.RemoveHandler (InitCompleteEvent, value); }
1832         }
1833         
1834         [EditorBrowsable (EditorBrowsableState.Advanced)]
1835         public event EventHandler LoadComplete {
1836                 add {
1837                         event_mask |= loadcomplete_mask;
1838                         Events.AddHandler (LoadCompleteEvent, value);
1839                 }
1840                 remove { Events.RemoveHandler (LoadCompleteEvent, value); }
1841         }
1842         
1843         public event EventHandler PreInit {
1844                 add {
1845                         event_mask |= preinit_mask;
1846                         Events.AddHandler (PreInitEvent, value);
1847                 }
1848                 remove { Events.RemoveHandler (PreInitEvent, value); }
1849         }
1850         
1851         [EditorBrowsable (EditorBrowsableState.Advanced)]
1852         public event EventHandler PreLoad {
1853                 add {
1854                         event_mask |= preload_mask;
1855                         Events.AddHandler (PreLoadEvent, value);
1856                 }
1857                 remove { Events.RemoveHandler (PreLoadEvent, value); }
1858         }
1859         
1860         [EditorBrowsable (EditorBrowsableState.Advanced)]
1861         public event EventHandler PreRenderComplete {
1862                 add {
1863                         event_mask |= prerendercomplete_mask;
1864                         Events.AddHandler (PreRenderCompleteEvent, value);
1865                 }
1866                 remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
1867         }
1868         
1869         [EditorBrowsable (EditorBrowsableState.Advanced)]
1870         public event EventHandler SaveStateComplete {
1871                 add {
1872                         event_mask |= savestatecomplete_mask;
1873                         Events.AddHandler (SaveStateCompleteEvent, value);
1874                 }
1875                 remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
1876         }
1877         
1878         protected virtual void OnInitComplete (EventArgs e)
1879         {
1880                 if ((event_mask & initcomplete_mask) != 0) {
1881                         EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
1882                         if (eh != null) eh (this, e);
1883                 }
1884         }
1885         
1886         protected virtual void OnLoadComplete (EventArgs e)
1887         {
1888                 if ((event_mask & loadcomplete_mask) != 0) {
1889                         EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
1890                         if (eh != null) eh (this, e);
1891                 }
1892         }
1893         
1894         protected virtual void OnPreInit (EventArgs e)
1895         {
1896                 if ((event_mask & preinit_mask) != 0) {
1897                         EventHandler eh = (EventHandler) (Events [PreInitEvent]);
1898                         if (eh != null) eh (this, e);
1899                 }
1900         }
1901         
1902         protected virtual void OnPreLoad (EventArgs e)
1903         {
1904                 if ((event_mask & preload_mask) != 0) {
1905                         EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
1906                         if (eh != null) eh (this, e);
1907                 }
1908         }
1909         
1910         protected virtual void OnPreRenderComplete (EventArgs e)
1911         {
1912                 if ((event_mask & prerendercomplete_mask) != 0) {
1913                         EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
1914                         if (eh != null) eh (this, e);
1915                 }
1916
1917                 if (Form == null)
1918                         return;
1919                 if (!Form.DetermineRenderUplevel ())
1920                         return;
1921
1922                 /* figure out if we have some control we're going to focus */
1923                 if (String.IsNullOrEmpty (_focusedControlID)) {
1924                         _focusedControlID = Form.DefaultFocus;
1925                         if (String.IsNullOrEmpty (_focusedControlID))
1926                                 _focusedControlID = Form.DefaultButton;
1927                 }
1928
1929                         if (!String.IsNullOrEmpty (_focusedControlID)) {
1930                                 ClientScript.RegisterWebFormClientScript ();
1931                                 ClientScript.RegisterStartupScript ("HtmlForm-DefaultButton-StartupScript",
1932                                                                          String.Format ("<script type=\"text/javascript\">\n" +
1933                                                                                         "<!--\n" +
1934                                                                                         "WebForm_AutoFocus('{0}');// -->\n" +
1935                                                                                         "</script>\n", _focusedControlID));
1936                         }
1937
1938                         if (Form.SubmitDisabledControls && _hasEnabledControlArray) {
1939                                 ClientScript.RegisterWebFormClientScript ();
1940                                 ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
1941                                                                                  "WebForm_ReEnableControls(this);");
1942                         }
1943         }
1944
1945         internal void RegisterEnabledControl (Control control)
1946         {
1947                 if (Form == null || !Page.Form.SubmitDisabledControls || !Page.Form.DetermineRenderUplevel ())
1948                         return;
1949                 _hasEnabledControlArray = true;
1950                 Page.ClientScript.RegisterArrayDeclaration (EnabledControlArrayID, String.Format ("'{0}'", control.ClientID));
1951         }
1952         
1953         protected virtual void OnSaveStateComplete (EventArgs e)
1954         {
1955                 if ((event_mask & savestatecomplete_mask) != 0) {
1956                         EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
1957                         if (eh != null) eh (this, e);
1958                 }
1959         }
1960         
1961         public HtmlForm Form {
1962                 get { return _form; }
1963         }
1964         
1965         internal void RegisterForm (HtmlForm form)
1966         {
1967                 _form = form;
1968         }
1969         
1970         [BrowsableAttribute (false)]
1971         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1972         public Page PreviousPage {
1973                 get {
1974                         if (_doLoadPreviousPage) {
1975                                 _doLoadPreviousPage = false;
1976                                 LoadPreviousPageReference ();
1977                         }
1978                         return previousPage;
1979                 }
1980         }
1981
1982         
1983         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1984         [BrowsableAttribute (false)]
1985         public bool IsCallback {
1986                 get { return isCallback; }
1987         }
1988         
1989         [BrowsableAttribute (false)]
1990         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1991         public bool IsCrossPagePostBack {
1992                 get { return isCrossPagePostBack; }
1993         }
1994
1995         public new virtual char IdSeparator {
1996                 get {
1997                         //TODO: why override?
1998                         return base.IdSeparator;
1999                 }
2000         }
2001         
2002         string ProcessCallbackData ()
2003         {
2004                 string callbackTarget = _requestValueCollection [CallbackSourceID];
2005                 if (callbackTarget == null || callbackTarget.Length == 0)
2006                         throw new HttpException ("Callback target not provided.");
2007
2008                 Control targetControl = FindControl (callbackTarget);
2009                 ICallbackEventHandler target = targetControl as ICallbackEventHandler;
2010                 if (target == null)
2011                         throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
2012
2013                 string callbackEventError = String.Empty;
2014                 string callBackResult;
2015                 string callbackArgument = _requestValueCollection [CallbackArgumentID];
2016
2017                 try {
2018                         target.RaiseCallbackEvent (callbackArgument);
2019                 }
2020                 catch (Exception ex) {
2021                         callbackEventError = String.Format ("e{0}", ex.Message);
2022                 }
2023                 
2024                 try {
2025                         callBackResult = target.GetCallbackResult ();
2026                 }
2027                 catch (Exception ex) {
2028                         return String.Format ("e{0}", ex.Message);
2029                 }
2030                 
2031                 string eventValidation = ClientScript.GetEventValidationStateFormatted ();
2032                 return String.Format ("{0}{1}|{2}{3}", callbackEventError, eventValidation == null ? 0 : eventValidation.Length, eventValidation, callBackResult);
2033         }
2034
2035         [BrowsableAttribute (false)]
2036         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
2037         public HtmlHead Header {
2038                 get { return htmlHeader; }
2039         }
2040         
2041         internal void SetHeader (HtmlHead header)
2042         {
2043                 htmlHeader = header;
2044                 if (_title != null) {
2045                         htmlHeader.Title = _title;
2046                         _title = null;
2047                 }
2048         }
2049
2050         protected bool AsyncMode {
2051                 get { return asyncMode; }
2052                 set { asyncMode = value; }
2053         }
2054
2055         public TimeSpan AsyncTimeout {
2056                 get { return asyncTimeout; }
2057                 set { asyncTimeout = value; }
2058         }
2059
2060         public bool IsAsync {
2061                 get { return AsyncMode; }
2062         }
2063         
2064         [MonoTODO ("Not Implemented")]
2065         protected internal virtual string UniqueFilePathSuffix {
2066                 get {
2067                         throw new NotImplementedException ();
2068                 }
2069         }
2070
2071         [MonoTODO ("Not Implemented")]
2072         public int MaxPageStateFieldLength {
2073                 get {
2074                         throw new NotImplementedException ();
2075                 }
2076                 set {
2077                         throw new NotImplementedException ();
2078                 }
2079         }
2080
2081         public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler)
2082         {
2083                 AddOnPreRenderCompleteAsync (beginHandler, endHandler, null);
2084         }
2085
2086         public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler, Object state)
2087         {
2088                 if (!IsAsync) {
2089                         throw new InvalidOperationException ("AddOnPreRenderCompleteAsync called and Page.IsAsync == false");
2090                 }
2091
2092                 if (_lifeCycle >= PageLifeCycle.PreRender) {
2093                         throw new InvalidOperationException ("AddOnPreRenderCompleteAsync can only be called before PreRender.");
2094                 }
2095
2096                 if (beginHandler == null) {
2097                         throw new ArgumentNullException ("beginHandler");
2098                 }
2099
2100                 if (endHandler == null) {
2101                         throw new ArgumentNullException ("endHandler");
2102                 }
2103
2104                 RegisterAsyncTask (new PageAsyncTask (beginHandler, endHandler, null, state, false));
2105         }
2106
2107         private List<PageAsyncTask> ParallelTasks {
2108                 get
2109                 {
2110                         if (parallelTasks == null) {
2111                                 parallelTasks = new List<PageAsyncTask>();
2112                         }
2113                         return parallelTasks;
2114                 }
2115         }
2116
2117         private List<PageAsyncTask> SerialTasks {
2118                 get {
2119                         if (serialTasks == null) {
2120                                 serialTasks = new List<PageAsyncTask> ();
2121                         }
2122                         return serialTasks;
2123                 }
2124         }
2125
2126         public void RegisterAsyncTask (PageAsyncTask task) 
2127         {
2128                 if (task == null) {
2129                         throw new ArgumentNullException ("task");
2130                 }
2131
2132                 if (task.ExecuteInParallel) {
2133                         ParallelTasks.Add (task);
2134                 }
2135                 else {
2136                         SerialTasks.Add (task);
2137                 }
2138         }
2139
2140         public void ExecuteRegisteredAsyncTasks ()
2141         {
2142                 if ((parallelTasks == null || parallelTasks.Count == 0) &&
2143                         (serialTasks == null || serialTasks.Count == 0)){
2144                         return;
2145                 }
2146
2147                 if (parallelTasks != null) {
2148                         DateTime startExecution = DateTime.Now;
2149                         List<PageAsyncTask> localParallelTasks = parallelTasks;
2150                         parallelTasks = null; // Shouldn't execute tasks twice
2151                         List<IAsyncResult> asyncResults = new List<IAsyncResult>();
2152                         foreach (PageAsyncTask parallelTask in localParallelTasks) {
2153                                 IAsyncResult result = parallelTask.BeginHandler (this, EventArgs.Empty, new AsyncCallback (EndAsyncTaskCallback), parallelTask.State);
2154                                 if (result.CompletedSynchronously) {
2155                                         parallelTask.EndHandler (result);
2156                                 }
2157                                 else {
2158                                         asyncResults.Add (result);
2159                                 }
2160                         }
2161
2162                         if (asyncResults.Count > 0) {
2163 #if TARGET_JVM
2164                                 TimeSpan timeout = AsyncTimeout;
2165                                 long t1 = DateTime.Now.Ticks;
2166                                 bool signalled = true;
2167                                 for (int i = 0; i < asyncResults.Count; i++) {
2168                                         if (asyncResults [i].IsCompleted)
2169                                                 continue;
2170
2171                                         if (signalled)
2172                                                 signalled = asyncResults [i].AsyncWaitHandle.WaitOne (timeout, false);
2173
2174                                         if (signalled) {
2175                                                 long t2 = DateTime.Now.Ticks;
2176                                                 timeout = AsyncTimeout - TimeSpan.FromTicks (t2 - t1);
2177                                                 if (timeout.Ticks <= 0)
2178                                                         signalled = false;
2179                                         }
2180                                         else {
2181                                                 localParallelTasks [i].TimeoutHandler (asyncResults [i]);
2182                                         }
2183                                 }
2184 #else
2185                                 WaitHandle [] waitArray = new WaitHandle [asyncResults.Count];
2186                                 int i = 0;
2187                                 for (i = 0; i < asyncResults.Count; i++) {
2188                                         waitArray [i] = asyncResults [i].AsyncWaitHandle;
2189                                 }
2190                                 bool allSignalled = WaitHandle.WaitAll (waitArray, AsyncTimeout, false);
2191                                 if (!allSignalled) {
2192                                         for (i = 0; i < asyncResults.Count; i++) {
2193                                                 if (!asyncResults [i].IsCompleted) {
2194                                                         localParallelTasks [i].TimeoutHandler (asyncResults [i]);
2195                                                 }
2196                                         }
2197                                 }
2198 #endif
2199                         }
2200                         DateTime endWait = DateTime.Now;
2201                         TimeSpan elapsed = endWait - startExecution;
2202                         if (elapsed <= AsyncTimeout) {
2203                                 AsyncTimeout -= elapsed;
2204                         }
2205                         else {
2206                                 AsyncTimeout = TimeSpan.FromTicks(0);
2207                         }
2208                 }
2209
2210                 if (serialTasks != null) {
2211                         List<PageAsyncTask> localSerialTasks = serialTasks;
2212                         serialTasks = null; // Shouldn't execute tasks twice
2213                         foreach (PageAsyncTask serialTask in localSerialTasks) {
2214                                 DateTime startExecution = DateTime.Now;
2215
2216                                 IAsyncResult result = serialTask.BeginHandler (this, EventArgs.Empty, new AsyncCallback (EndAsyncTaskCallback), serialTask);
2217                                 if (result.CompletedSynchronously) {
2218                                         serialTask.EndHandler (result);
2219                                 }
2220                                 else {
2221                                         bool done = result.AsyncWaitHandle.WaitOne (AsyncTimeout, false);
2222                                         if (!done && !result.IsCompleted) {
2223                                                 serialTask.TimeoutHandler (result);
2224                                         }
2225                                 }
2226                                 DateTime endWait = DateTime.Now;
2227                                 TimeSpan elapsed = endWait - startExecution;
2228                                 if (elapsed <= AsyncTimeout) {
2229                                         AsyncTimeout -= elapsed;
2230                                 }
2231                                 else {
2232                                         AsyncTimeout = TimeSpan.FromTicks (0);
2233                                 }
2234                         }
2235                 }
2236                 AsyncTimeout = TimeSpan.FromSeconds (DefaultAsyncTimeout);
2237         }
2238
2239         void EndAsyncTaskCallback (IAsyncResult result) 
2240         {
2241                 PageAsyncTask task = (PageAsyncTask)result.AsyncState;
2242                 task.EndHandler (result);
2243         }
2244
2245         public static HtmlTextWriter CreateHtmlTextWriterFromType (TextWriter tw, Type writerType)
2246         {
2247                 Type htmlTextWriterType = typeof (HtmlTextWriter);
2248                 
2249                 if (!htmlTextWriterType.IsAssignableFrom (writerType)) {
2250                         throw new HttpException (String.Format ("Type '{0}' cannot be assigned to HtmlTextWriter", writerType.FullName));
2251                 }
2252
2253                 ConstructorInfo constructor = writerType.GetConstructor (new Type [] { typeof (TextWriter) });
2254                 if (constructor == null) {
2255                         throw new HttpException (String.Format ("Type '{0}' does not have a consturctor that takes a TextWriter as parameter", writerType.FullName));
2256                 }
2257
2258                 return (HtmlTextWriter) Activator.CreateInstance(writerType, tw);
2259         }
2260
2261         public ViewStateEncryptionMode ViewStateEncryptionMode {
2262                 get { return viewStateEncryptionMode; }
2263                 set { viewStateEncryptionMode = value; }
2264         }
2265
2266         public void RegisterRequiresViewStateEncryption ()
2267         {
2268                 controlRegisteredForViewStateEncryption = true;
2269         }
2270
2271         private static byte [] AES_IV = null;
2272         private static byte [] TripleDES_IV = null;
2273         private static object locker = new object ();
2274         private static bool isEncryptionInitialized = false;
2275
2276         private static void InitializeEncryption () 
2277         {
2278                 if (isEncryptionInitialized) {
2279                         return;
2280                 }
2281
2282                 lock (locker) {
2283                         if (isEncryptionInitialized) {
2284                                 return;
2285                         }
2286
2287                         string iv_string = "0BA48A9E-736D-40f8-954B-B2F62241F282";
2288                         AES_IV = new byte [16];
2289                         TripleDES_IV = new byte [8];
2290
2291                         int i;
2292                         for (i = 0; i < AES_IV.Length; i++) {
2293                                 AES_IV [i] = (byte) iv_string [i];
2294                         }
2295
2296                         for (i = 0; i < TripleDES_IV.Length; i++) {
2297                                 TripleDES_IV [i] = (byte) iv_string [i];
2298                         }
2299
2300                         isEncryptionInitialized = true;
2301                 }
2302         }
2303
2304         internal ICryptoTransform GetCryptoTransform (CryptoStreamMode cryptoStreamMode) 
2305         {
2306                 ICryptoTransform transform = null;
2307                 MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
2308                 byte [] vk = config.ValidationKeyBytes;
2309
2310                 switch (config.Validation) {
2311                 case MachineKeyValidation.SHA1:
2312                         transform = SHA1.Create ();
2313                         break;
2314
2315                 case MachineKeyValidation.MD5:
2316                         transform = MD5.Create ();
2317                         break;
2318
2319                 case MachineKeyValidation.AES:
2320                         if (cryptoStreamMode == CryptoStreamMode.Read){
2321                                 transform = Rijndael.Create().CreateDecryptor(vk, AES_IV);
2322                         } else {
2323                                 transform = Rijndael.Create().CreateEncryptor(vk, AES_IV);
2324                         }
2325                         break;
2326
2327                 case MachineKeyValidation.TripleDES:
2328                         if (cryptoStreamMode == CryptoStreamMode.Read){
2329                                 transform = TripleDES.Create().CreateDecryptor(vk, TripleDES_IV);
2330                         } else {
2331                                 transform = TripleDES.Create().CreateEncryptor(vk, TripleDES_IV);
2332                         }
2333                         break;
2334                 }
2335
2336                 return transform;
2337         }
2338
2339         internal bool NeedViewStateEncryption {
2340                 get {
2341                         return (ViewStateEncryptionMode == ViewStateEncryptionMode.Always ||
2342                                         (ViewStateEncryptionMode == ViewStateEncryptionMode.Auto &&
2343                                          controlRegisteredForViewStateEncryption));
2344
2345                 }
2346         }
2347
2348         void ApplyMasterPage ()
2349         {
2350                 if (masterPageFile != null && masterPageFile.Length > 0) {
2351                         ArrayList appliedMasterPageFiles = new ArrayList ();
2352
2353                         if (Master != null) {
2354                                 MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
2355
2356                                 Master.Page = this;
2357                                 Controls.Clear ();
2358                                 Controls.Add (Master);
2359                         }
2360                 }
2361         }
2362
2363         [DefaultValueAttribute ("")]
2364         public virtual string MasterPageFile {
2365                 get { return masterPageFile; }
2366                 set {
2367                         masterPageFile = value;
2368                         masterPage = null;
2369                 }
2370         }
2371         
2372         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
2373         [BrowsableAttribute (false)]
2374         public MasterPage Master {
2375                 get {
2376                         if (Context == null || String.IsNullOrEmpty (masterPageFile))
2377                                 return null;
2378
2379                         if (masterPage == null)
2380                                 masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
2381
2382                         return masterPage;
2383                 }
2384         }
2385         
2386         public void SetFocus (string clientID)
2387         {
2388                 if (String.IsNullOrEmpty (clientID))
2389                         throw new ArgumentNullException ("control");
2390
2391                 if (_lifeCycle > PageLifeCycle.PreRender)
2392                         throw new InvalidOperationException ("SetFocus can only be called before and during PreRender.");
2393
2394                 if(Form==null)
2395                         throw new InvalidOperationException ("A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property.");
2396
2397                 _focusedControlID = clientID;
2398         }
2399
2400         public void SetFocus (Control control)
2401         {
2402                 if (control == null)
2403                         throw new ArgumentNullException ("control");
2404
2405                 SetFocus (control.ClientID);
2406         }
2407         
2408         [EditorBrowsable (EditorBrowsableState.Advanced)]
2409         public void RegisterRequiresControlState (Control control)
2410         {
2411                 if (control == null)
2412                         throw new ArgumentNullException ("control");
2413
2414                 if (RequiresControlState (control))
2415                         return;
2416
2417                 if (requireStateControls == null)
2418                         requireStateControls = new ArrayList ();
2419                 int n = requireStateControls.Add (control);
2420
2421                 if (_savedControlState == null || n >= _savedControlState.Length) 
2422                         return;
2423
2424                 for (Control parent = control.Parent; parent != null; parent = parent.Parent)
2425                         if (parent.IsChildControlStateCleared)
2426                                 return;
2427
2428                 object state = _savedControlState [n];
2429                 if (state != null)
2430                         control.LoadControlState (state);
2431         }
2432         
2433         public bool RequiresControlState (Control control)
2434         {
2435                 if (requireStateControls == null) return false;
2436                 return requireStateControls.Contains (control);
2437         }
2438         
2439         [EditorBrowsable (EditorBrowsableState.Advanced)]
2440         public void UnregisterRequiresControlState (Control control)
2441         {
2442                 if (requireStateControls != null)
2443                         requireStateControls.Remove (control);
2444         }
2445         
2446         public ValidatorCollection GetValidators (string validationGroup)
2447         {
2448                 string valgr = validationGroup;
2449                 if (valgr == null)
2450                         valgr = String.Empty;
2451
2452                 if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
2453                 ValidatorCollection col = _validatorsByGroup [valgr] as ValidatorCollection;
2454                 if (col == null) {
2455                         col = new ValidatorCollection ();
2456                         _validatorsByGroup [valgr] = col;
2457                 }
2458                 return col;
2459         }
2460         
2461         public virtual void Validate (string validationGroup)
2462         {
2463                 is_validated = true;
2464                 if (validationGroup == null)
2465                         ValidateCollection (_validatorsByGroup [String.Empty] as ValidatorCollection);
2466                 else if (_validatorsByGroup != null) {
2467                         ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
2468                 }
2469         }
2470
2471         object SavePageControlState ()
2472         {
2473                 if (requireStateControls == null) return null;
2474                 object[] state = new object [requireStateControls.Count];
2475                 
2476                 bool allNull = true;
2477                 for (int n=0; n<state.Length; n++) {
2478                         state [n] = ((Control) requireStateControls [n]).SaveControlState ();
2479                         if (state [n] != null) allNull = false;
2480                 }
2481                 if (allNull) return null;
2482                 else return state;
2483         }
2484         
2485         void LoadPageControlState (object data)
2486         {
2487                 _savedControlState = (object []) data;
2488                 
2489                 if (requireStateControls == null) return;
2490
2491                 int max = Math.Min (requireStateControls.Count, _savedControlState != null ? _savedControlState.Length : requireStateControls.Count);
2492                 for (int n=0; n < max; n++) {
2493                         Control ctl = (Control) requireStateControls [n];
2494                         ctl.LoadControlState (_savedControlState != null ? _savedControlState [n] : null);
2495                 }
2496         }
2497
2498         void LoadPreviousPageReference ()
2499         {
2500                 if (_requestValueCollection != null) {
2501                         string prevPage = _requestValueCollection [PreviousPageID];
2502                         if (prevPage != null) {
2503                                 previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
2504                                 previousPage.ProcessCrossPagePostBack (_context);
2505                         } 
2506                 }
2507         }
2508
2509
2510         Hashtable contentTemplates;
2511         [EditorBrowsable (EditorBrowsableState.Never)]
2512         protected internal void AddContentTemplate (string templateName, ITemplate template)
2513         {
2514                 if (contentTemplates == null)
2515                         contentTemplates = new Hashtable ();
2516                 contentTemplates [templateName] = template;
2517         }
2518
2519         PageTheme _pageTheme;
2520         internal PageTheme PageTheme {
2521                 get { return _pageTheme; }
2522         }
2523
2524         PageTheme _styleSheetPageTheme;
2525         internal PageTheme StyleSheetPageTheme {
2526                 get { return _styleSheetPageTheme; }
2527         }
2528
2529         Stack dataItemCtx;
2530         
2531         internal void PushDataItemContext (object o) {
2532                 if (dataItemCtx == null)
2533                         dataItemCtx = new Stack ();
2534                 
2535                 dataItemCtx.Push (o);
2536         }
2537         
2538         internal void PopDataItemContext () {
2539                 if (dataItemCtx == null)
2540                         throw new InvalidOperationException ();
2541                 
2542                 dataItemCtx.Pop ();
2543         }
2544         
2545         public object GetDataItem() {
2546                 if (dataItemCtx == null || dataItemCtx.Count == 0)
2547                         throw new InvalidOperationException ("No data item");
2548                 
2549                 return dataItemCtx.Peek ();
2550         }
2551
2552         protected internal override void OnInit (EventArgs e)
2553         {
2554                 base.OnInit (e);
2555
2556                 ArrayList themes = new ArrayList();
2557
2558                 if (StyleSheetPageTheme != null && StyleSheetPageTheme.GetStyleSheets () != null)
2559                         themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
2560                 if (PageTheme != null && PageTheme.GetStyleSheets () != null)
2561                         themes.AddRange (PageTheme.GetStyleSheets ());
2562
2563                 if (themes.Count > 0 && Header == null)
2564                         throw new InvalidOperationException ("Using themed css files requires a header control on the page.");
2565
2566                 foreach (string lss in themes) {
2567                         HtmlLink hl = new HtmlLink ();
2568                         hl.Href = ResolveUrl (lss);
2569                         hl.Attributes["type"] = "text/css";
2570                         hl.Attributes["rel"] = "stylesheet";
2571                         Header.Controls.Add (hl);
2572                 }
2573         }
2574
2575         #endif
2576
2577 #if NET_2_0
2578         [MonoTODO ("Not implemented.  Only used by .net aspx parser")]
2579         protected object GetWrappedFileDependencies (string [] list)
2580         {
2581                 return list;
2582         }
2583
2584         [MonoTODO ("Does nothing.  Used by .net aspx parser")]
2585         protected virtual void InitializeCulture ()
2586         {
2587         }
2588
2589         [MonoTODO ("Does nothing. Used by .net aspx parser")]
2590         protected internal void AddWrappedFileDependencies (object virtualFileDependencies)
2591         {
2592         }
2593 #endif
2594 }
2595 }