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