move the WriteClientScriptIncludes to the top of the form, according to the MSDN
[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 PostBackFunctionName {
444                 get {
445                         return "__doPostBack";
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 = new CultureInfo (languages[0]);
714                         } catch {
715                         }
716                         
717                         if (ret == null)
718                                 ret = deflt;
719                 } else
720                         ret = new CultureInfo (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                         WebROCollection c = (WebROCollection) coll;
766                         allow_load = !c.GotID;
767                         if (allow_load) {
768                                 c.ID = GetTypeHashCode ();
769                         } else {
770                                 allow_load = (c.ID == GetTypeHashCode ());
771                         }
772                 } else  {
773                         coll = req.QueryString;
774                 }
775
776 #if TARGET_J2EE
777                 coll = LoadViewStateForPortlet((WebROCollection)coll);
778 #endif
779
780                 if (coll != null && coll ["__VIEWSTATE"] == null && coll ["__EVENTTARGET"] == null)
781                         return null;
782
783                 return coll;
784         }
785
786 #if NET_2_0
787         public override Control FindControl (string id) {
788                 if (id == ID)
789                         return this;
790                 else
791                         return base.FindControl (id);
792         }
793 #endif
794
795 #if NET_2_0
796         [Obsolete]
797 #endif
798         [EditorBrowsable (EditorBrowsableState.Advanced)]
799         public string GetPostBackClientEvent (Control control, string argument)
800         {
801                 return scriptManager.GetPostBackEventReference (control, argument);
802         }
803
804 #if NET_2_0
805         [Obsolete]
806 #endif
807         [EditorBrowsable (EditorBrowsableState.Advanced)]
808         public string GetPostBackClientHyperlink (Control control, string argument)
809         {
810                 return scriptManager.GetPostBackClientHyperlink (control, argument);
811         }
812
813 #if NET_2_0
814         [Obsolete]
815 #endif
816         [EditorBrowsable (EditorBrowsableState.Advanced)]
817         public string GetPostBackEventReference (Control control)
818         {
819                 return scriptManager.GetPostBackEventReference (control, "");
820         }
821
822 #if NET_2_0
823         [Obsolete]
824 #endif
825         [EditorBrowsable (EditorBrowsableState.Advanced)]
826         public string GetPostBackEventReference (Control control, string argument)
827         {
828                 return scriptManager.GetPostBackEventReference (control, argument);
829         }
830
831         internal void RequiresPostBackScript ()
832         {
833                 requiresPostBackScript = true;
834         }
835
836         [EditorBrowsable (EditorBrowsableState.Never)]
837         public virtual int GetTypeHashCode ()
838         {
839                 return 0;
840         }
841
842 #if NET_2_0
843     [MonoTODO("The following properties of OutputCacheParameters are silently ignored: CacheProfile, NoStore, SqlDependency")]
844     protected internal virtual void InitOutputCache(OutputCacheParameters cacheSettings)
845     {
846         if (cacheSettings.Enabled)
847             InitOutputCache(cacheSettings.Duration,
848                 cacheSettings.VaryByHeader,
849                 cacheSettings.VaryByCustom,
850                 cacheSettings.Location,
851                 cacheSettings.VaryByParam);
852     }
853 #endif
854
855         [EditorBrowsable (EditorBrowsableState.Never)]
856         protected virtual void InitOutputCache (int duration,
857                                                 string varyByHeader,
858                                                 string varyByCustom,
859                                                 OutputCacheLocation location,
860                                                 string varyByParam)
861         {
862                 HttpCachePolicy cache = _context.Response.Cache;
863                 bool set_vary = false;
864
865                 switch (location) {
866                 case OutputCacheLocation.Any:
867                         cache.SetCacheability (HttpCacheability.Public);
868                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
869                         cache.SetLastModified (_context.Timestamp);
870                         set_vary = true;
871                         break;
872                 case OutputCacheLocation.Client:
873                         cache.SetCacheability (HttpCacheability.Private);
874                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
875                         cache.SetLastModified (_context.Timestamp);
876                         break;
877                 case OutputCacheLocation.Downstream:
878                         cache.SetCacheability (HttpCacheability.Public);
879                         cache.SetMaxAge (new TimeSpan (0, 0, duration));                
880                         cache.SetLastModified (_context.Timestamp);
881                         break;
882                 case OutputCacheLocation.Server:                        
883                         cache.SetCacheability (HttpCacheability.Server);
884                         set_vary = true;
885                         break;
886                 case OutputCacheLocation.None:
887                         break;
888                 }
889
890                 if (set_vary) {
891                         if (varyByCustom != null)
892                                 cache.SetVaryByCustom (varyByCustom);
893
894                         if (varyByParam != null && varyByParam.Length > 0) {
895                                 string[] prms = varyByParam.Split (';');
896                                 foreach (string p in prms)
897                                         cache.VaryByParams [p.Trim ()] = true;
898                                 cache.VaryByParams.IgnoreParams = false;
899                         } else {
900                                 cache.VaryByParams.IgnoreParams = true;
901                         }
902                         
903                         if (varyByHeader != null && varyByHeader.Length > 0) {
904                                 string[] hdrs = varyByHeader.Split (';');
905                                 foreach (string h in hdrs)
906                                         cache.VaryByHeaders [h.Trim ()] = true;
907                         }
908                 }
909                         
910                 cache.Duration = duration;
911                 cache.SetExpires (_context.Timestamp.AddSeconds (duration));
912         }
913
914 #if NET_2_0
915         [Obsolete]
916 #else
917         [EditorBrowsable (EditorBrowsableState.Advanced)]
918 #endif
919         public bool IsClientScriptBlockRegistered (string key)
920         {
921                 return scriptManager.IsClientScriptBlockRegistered (key);
922         }
923
924 #if NET_2_0
925         [Obsolete]
926 #else
927         [EditorBrowsable (EditorBrowsableState.Advanced)]
928 #endif
929         public bool IsStartupScriptRegistered (string key)
930         {
931                 return scriptManager.IsStartupScriptRegistered (key);
932         }
933
934         public string MapPath (string virtualPath)
935         {
936                 return Request.MapPath (virtualPath);
937         }
938
939 #if NET_2_0
940         protected internal override void Render (HtmlTextWriter writer) {
941                 if (MaintainScrollPositionOnPostBack) {
942                         RequiresPostBackScript ();
943
944                         string scriptUrl = ClientScript.GetWebResourceUrl (typeof (Page), "MaintainScrollPositionOnPostBack.js");
945
946                         ClientScript.RegisterClientScriptInclude (typeof (Page), "MaintainScrollPositionOnPostBack.js", scriptUrl);
947
948                         ClientScript.RegisterHiddenField (ScrollPositionXID, Request [ScrollPositionXID]);
949                         ClientScript.RegisterHiddenField (ScrollPositionYID, Request [ScrollPositionYID]);
950                         
951                         StringBuilder script = new StringBuilder ();
952                         script.AppendLine ("<script type=\"text/javascript\">");
953                         script.AppendLine ("<!--");
954                         script.AppendLine ("theForm.oldSubmit = theForm.submit");
955                         script.AppendLine ("theForm.submit = WebForm_SaveScrollPositionSubmit");
956                         script.AppendLine ("theForm.oldOnSubmit = theForm.onsubmit");
957                         script.AppendLine ("theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit");
958                         if (IsPostBack) {
959                                 script.AppendLine ("theForm.oldOnLoad = window.onload");
960                                 script.AppendLine ("window.onload = WebForm_RestoreScrollPosition");
961                         }
962                         script.AppendLine ("// -->");
963                         script.AppendLine ("</script>");
964                         
965                         ClientScript.RegisterStartupScript (typeof (Page), "MaintainScrollPositionOnPostBackStartup", script.ToString());
966                 }
967                 base.Render (writer);
968         }
969 #endif
970
971         private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
972         {
973                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
974                 writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
975                 writer.WriteLine ();
976                 writer.WriteLine ("<script language=\"javascript\">");
977                 writer.WriteLine ("<!--");
978
979                 writer.WriteLine ("\tvar theForm;\n\tif (document.getElementById) {{ theForm = document.getElementById ('{0}'); }}", formUniqueID);
980                 writer.WriteLine ("\telse {{ theForm = document.{0}; }}", formUniqueID);
981                 writer.WriteLine ("\tfunction " + PostBackFunctionName + "(eventTarget, eventArgument) {");
982                 writer.WriteLine ("\t\tif(document.ValidatorOnSubmit && !ValidatorOnSubmit()) return;");
983                 writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
984                 writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
985                 writer.WriteLine ("\t\ttheForm.submit();");
986                 writer.WriteLine ("\t}");
987                 writer.WriteLine ("// -->");
988                 writer.WriteLine ("</script>");
989         }
990
991         internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
992         {
993                 if (renderingForm)
994                         throw new HttpException ("Only 1 HtmlForm is allowed per page.");
995
996                 renderingForm = true;
997                 writer.WriteLine ();
998
999                 if (handleViewState)
1000                         scriptManager.RegisterHiddenField ("__VIEWSTATE", _savedViewState);
1001
1002                 scriptManager.WriteHiddenFields (writer);
1003                 if (requiresPostBackScript) {
1004                         RenderPostBackScript (writer, formUniqueID);
1005                         postBackScriptRendered = true;
1006                 }
1007                 scriptManager.WriteClientScriptIncludes (writer);
1008                 scriptManager.WriteClientScriptBlocks (writer);
1009         }
1010
1011         internal IStateFormatter GetFormatter ()
1012         {
1013                 return new ObjectStateFormatter (this);
1014         }
1015
1016         internal string GetSavedViewState ()
1017         {
1018                 return _savedViewState;
1019         }
1020
1021         internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
1022         {
1023                 scriptManager.WriteArrayDeclares (writer);
1024
1025                 if (!postBackScriptRendered && requiresPostBackScript)
1026                         RenderPostBackScript (writer, formUniqueID);
1027                 
1028 #if NET_2_0
1029                 scriptManager.SaveEventValidationState ();
1030                 scriptManager.WriteExpandoAttributes (writer);
1031 #endif
1032                 scriptManager.WriteHiddenFields (writer);
1033                 scriptManager.WriteStartupScriptBlocks (writer);
1034                 renderingForm = false;
1035                 postBackScriptRendered = false;
1036         }
1037
1038         private void ProcessPostData (NameValueCollection data, bool second)
1039         {
1040                 if (data != null) {
1041
1042                 Hashtable used = new Hashtable ();
1043                 foreach (string id in data.AllKeys){
1044                         if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
1045                                 continue;
1046
1047                         string real_id = id;
1048                         int dot = real_id.IndexOf ('.');
1049                         if (dot >= 1)
1050                                 real_id = real_id.Substring (0, dot);
1051                         
1052                         if (real_id == null || used.ContainsKey (real_id))
1053                                 continue;
1054
1055                         used.Add (real_id, real_id);
1056
1057                         Control ctrl = FindControl (real_id);
1058                         if (ctrl != null){
1059                                 IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
1060                                 IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
1061
1062                                 if (pbdh == null) {
1063                                         if (pbeh != null)
1064                                                 RegisterRequiresRaiseEvent (pbeh);
1065                                         continue;
1066                                 }
1067                 
1068                                 if (pbdh.LoadPostData (real_id, data) == true) {
1069                                         if (requiresPostDataChanged == null)
1070                                                 requiresPostDataChanged = new ArrayList ();
1071                                         requiresPostDataChanged.Add (pbdh);
1072                                 }
1073                                 
1074                                 if (_requiresPostBackCopy != null)
1075                                         _requiresPostBackCopy.Remove (real_id);
1076
1077                         } else if (!second) {
1078                                 if (secondPostData == null)
1079                                         secondPostData = new NameValueCollection ();
1080                                 secondPostData.Add (real_id, data [id]);
1081                         }
1082                 }
1083                 }
1084
1085                 ArrayList list1 = null;
1086                 if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
1087                         string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
1088                         foreach (string id in handlers) {
1089                                 IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
1090                                 if (pbdh != null) {                     
1091                                         _requiresPostBackCopy.Remove (id);
1092                                         if (pbdh.LoadPostData (id, data)) {
1093                                                 if (requiresPostDataChanged == null)
1094                                                         requiresPostDataChanged = new ArrayList ();
1095         
1096                                                 requiresPostDataChanged.Add (pbdh);
1097                                         }
1098                                 } else if (!second) {
1099                                         if (list1 == null)
1100                                                 list1 = new ArrayList ();
1101                                         list1.Add (id);
1102                                 }
1103                         }
1104                 }
1105                 _requiresPostBackCopy = second ? null : list1;
1106                 if (second)
1107                         secondPostData = null;
1108         }
1109
1110         [EditorBrowsable (EditorBrowsableState.Never)]
1111 #if NET_2_0 || TARGET_JVM
1112         public virtual void ProcessRequest (HttpContext context)
1113 #else
1114         public void ProcessRequest (HttpContext context)
1115 #endif
1116         {
1117 #if NET_2_0
1118                 _lifeCycle = PageLifeCycle.Unknown;
1119 #endif
1120                 _context = context;
1121                 if (clientTarget != null)
1122                         Request.ClientTarget = clientTarget;
1123
1124                 WireupAutomaticEvents ();
1125                 //-- Control execution lifecycle in the docs
1126
1127                 // Save culture information because it can be modified in FrameworkInitialize()
1128                 CultureInfo culture = Thread.CurrentThread.CurrentCulture;
1129                 CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
1130                 FrameworkInitialize ();
1131                 context.ErrorPage = _errorPage;
1132
1133                 try {
1134                         InternalProcessRequest ();
1135                 } catch (ThreadAbortException) {
1136                         // Do nothing, just ignore it by now.
1137                 } catch (Exception e) {
1138                         context.AddError (e); // OnError might access LastError
1139                         OnError (EventArgs.Empty);
1140                         context.ClearError (e);
1141                         // We want to remove that error, as we're rethrowing to stop
1142                         // further processing.
1143                         Trace.Warn ("Unhandled Exception", e.ToString (), e);
1144                         throw;
1145                 } finally {
1146                         try {
1147 #if NET_2_0
1148                                 _lifeCycle = PageLifeCycle.Unload;
1149 #endif
1150                                 RenderTrace ();
1151                                 UnloadRecursive (true);
1152 #if NET_2_0
1153                                 _lifeCycle = PageLifeCycle.End;
1154 #endif
1155                         } catch {}
1156                         if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
1157                                 Thread.CurrentThread.CurrentCulture = culture;
1158
1159                         if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
1160                                 Thread.CurrentThread.CurrentUICulture = uiculture;
1161                 }
1162         }
1163         
1164 #if NET_2_0
1165         internal void ProcessCrossPagePostBack (HttpContext context)
1166         {
1167                 isCrossPagePostBack = true;
1168                 ProcessRequest (context);
1169         }
1170 #endif
1171
1172         void InternalProcessRequest ()
1173         {
1174                 _requestValueCollection = this.DeterminePostBackMode();
1175
1176 #if NET_2_0
1177                 _lifeCycle = PageLifeCycle.Start;
1178                 // http://msdn2.microsoft.com/en-us/library/ms178141.aspx
1179                 if (_requestValueCollection != null) {
1180                         if (!isCrossPagePostBack && _requestValueCollection [PreviousPageID] != null && _requestValueCollection [PreviousPageID] != Request.FilePath) {
1181                                 _doLoadPreviousPage = true;
1182                         }
1183                         else {
1184                                 isCallback = _requestValueCollection [CallbackArgumentID] != null;
1185                                 isPostBack = !isCallback;
1186                         }
1187                 }
1188                 
1189                 // if request was transfered from other page - track Prev. Page
1190                 previousPage = _context.LastPage;
1191                 _context.LastPage = this;
1192
1193                 _lifeCycle = PageLifeCycle.PreInit;
1194                 OnPreInit (EventArgs.Empty);
1195
1196                 InitializeTheme ();
1197                 ApplyMasterPage ();
1198                 _lifeCycle = PageLifeCycle.Init;
1199 #endif
1200                 Trace.Write ("aspx.page", "Begin Init");
1201                 InitRecursive (null);
1202                 Trace.Write ("aspx.page", "End Init");
1203
1204 #if NET_2_0
1205                 _lifeCycle = PageLifeCycle.InitComplete;
1206                 OnInitComplete (EventArgs.Empty);
1207 #endif
1208                         
1209                 renderingForm = false;  
1210 #if NET_2_0
1211                 if (IsPostBack || IsCallback) {
1212                         _lifeCycle = PageLifeCycle.PreLoad;
1213                         if (_requestValueCollection != null)
1214                                 scriptManager.RestoreEventValidationState (_requestValueCollection [scriptManager.EventStateFieldName]);
1215 #else
1216                 if (IsPostBack) {
1217 #endif
1218                         Trace.Write ("aspx.page", "Begin LoadViewState");
1219                         LoadPageViewState ();
1220                         Trace.Write ("aspx.page", "End LoadViewState");
1221                         Trace.Write ("aspx.page", "Begin ProcessPostData");
1222                         ProcessPostData (_requestValueCollection, false);
1223                         Trace.Write ("aspx.page", "End ProcessPostData");
1224                 }
1225
1226 #if NET_2_0
1227                 OnPreLoad (EventArgs.Empty);
1228                 _lifeCycle = PageLifeCycle.Load;
1229 #endif
1230
1231                 LoadRecursive ();
1232 #if NET_2_0
1233                 if (IsPostBack || IsCallback) {
1234                         _lifeCycle = PageLifeCycle.ControlEvents;
1235 #else
1236                 if (IsPostBack) {
1237 #endif
1238                         Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
1239                         ProcessPostData (secondPostData, true);
1240                         Trace.Write ("aspx.page", "End ProcessPostData Second Try");
1241                         Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
1242                         RaiseChangedEvents ();
1243                         Trace.Write ("aspx.page", "End Raise ChangedEvents");
1244                         Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
1245                         RaisePostBackEvents ();
1246                         Trace.Write ("aspx.page", "End Raise PostBackEvent");
1247                 }
1248                 
1249 #if NET_2_0
1250                 _lifeCycle = PageLifeCycle.LoadComplete;
1251                 OnLoadComplete (EventArgs.Empty);
1252
1253                 if (IsCrossPagePostBack)
1254                         return;
1255
1256                 if (IsCallback) {
1257                         string result = ProcessCallbackData ();
1258                         HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
1259                         callbackOutput.Write (result);
1260                         callbackOutput.Flush ();
1261                         return;
1262                 }
1263
1264                 _lifeCycle = PageLifeCycle.PreRender;
1265 #endif
1266                 
1267                 Trace.Write ("aspx.page", "Begin PreRender");
1268                 PreRenderRecursiveInternal ();
1269                 Trace.Write ("aspx.page", "End PreRender");
1270                 
1271 #if NET_2_0
1272                 _lifeCycle = PageLifeCycle.PreRenderComplete;
1273                 OnPreRenderComplete (EventArgs.Empty);
1274 #endif
1275
1276                 Trace.Write ("aspx.page", "Begin SaveViewState");
1277                 SavePageViewState ();
1278                 Trace.Write ("aspx.page", "End SaveViewState");
1279                 
1280 #if NET_2_0
1281                 _lifeCycle = PageLifeCycle.SaveStateComplete;
1282                 OnSaveStateComplete (EventArgs.Empty);
1283 #endif
1284
1285 #if TARGET_J2EE
1286                 if (SaveViewStateForNextPortletRender())
1287                         return;
1288 #endif
1289
1290 #if NET_2_0
1291                 _lifeCycle = PageLifeCycle.Render;
1292 #endif
1293                 
1294                 //--
1295                 Trace.Write ("aspx.page", "Begin Render");
1296                 HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1297                 RenderControl (output);
1298                 Trace.Write ("aspx.page", "End Render");
1299         }
1300
1301         private void RenderTrace ()
1302         {
1303                 TraceManager traceManager = HttpRuntime.TraceManager;
1304
1305                 if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
1306                         return;
1307                 
1308                 Trace.SaveData ();
1309
1310                 if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput) 
1311                         return;
1312
1313                 if (!traceManager.LocalOnly || Context.Request.IsLocal) {
1314                         HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
1315                         Trace.Render (output);
1316                 }
1317         }
1318         
1319 #if NET_2_0
1320         bool CheckForValidationSupport (Control targetControl)
1321         {
1322                 if (targetControl == null)
1323                         return false;
1324                 Type type = targetControl.GetType ();
1325                 object[] attributes = type.GetCustomAttributes (true);
1326                 foreach (object attr in attributes)
1327                         if (attr is SupportsEventValidationAttribute)
1328                                 return true;
1329                 return false;
1330         }
1331 #endif
1332         
1333         void RaisePostBackEvents ()
1334         {
1335 #if NET_2_0
1336                 Control targetControl;
1337 #endif
1338                 if (requiresRaiseEvent != null) {
1339 #if NET_2_0
1340                         targetControl = requiresRaiseEvent as Control;
1341                         if (targetControl != null && CheckForValidationSupport (targetControl))
1342                                 scriptManager.ValidateEvent (targetControl.UniqueID, null);
1343 #endif
1344                         RaisePostBackEvent (requiresRaiseEvent, null);
1345                         return;
1346                 }
1347
1348                 NameValueCollection postdata = _requestValueCollection;
1349                 if (postdata == null)
1350                         return;
1351
1352                 string eventTarget = postdata [postEventSourceID];
1353                 if (eventTarget == null || eventTarget.Length == 0) {
1354                         Validate ();
1355                         return;
1356                 }
1357
1358 #if NET_2_0
1359                 targetControl = FindControl (eventTarget);
1360                 IPostBackEventHandler target = targetControl as IPostBackEventHandler;
1361 #else
1362                 IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
1363 #endif
1364                         
1365                 if (target == null)
1366                         return;
1367
1368                 string eventArgument = postdata [postEventArgumentID];
1369 #if NET_2_0
1370                 if (CheckForValidationSupport (targetControl))
1371                         scriptManager.ValidateEvent (targetControl.UniqueID, eventArgument);
1372 #endif
1373                 RaisePostBackEvent (target, eventArgument);
1374         }
1375
1376         internal void RaiseChangedEvents ()
1377         {
1378                 if (requiresPostDataChanged == null)
1379                         return;
1380
1381                 foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
1382                         ipdh.RaisePostDataChangedEvent ();
1383
1384                 requiresPostDataChanged = null;
1385         }
1386
1387         [EditorBrowsable (EditorBrowsableState.Advanced)]
1388         protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
1389         {
1390                 sourceControl.RaisePostBackEvent (eventArgument);
1391         }
1392         
1393 #if NET_2_0
1394         [Obsolete]
1395 #endif
1396         [EditorBrowsable (EditorBrowsableState.Advanced)]
1397         public void RegisterArrayDeclaration (string arrayName, string arrayValue)
1398         {
1399                 scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
1400         }
1401
1402 #if NET_2_0
1403         [Obsolete]
1404 #endif
1405         [EditorBrowsable (EditorBrowsableState.Advanced)]
1406         public virtual void RegisterClientScriptBlock (string key, string script)
1407         {
1408                 scriptManager.RegisterClientScriptBlock (key, script);
1409         }
1410
1411 #if NET_2_0
1412         [Obsolete]
1413 #endif
1414         [EditorBrowsable (EditorBrowsableState.Advanced)]
1415         public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
1416         {
1417                 scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
1418         }
1419
1420         [MonoTODO("Not implemented, Used in HtmlForm")]
1421         internal void RegisterClientScriptFile (string a, string b, string c)
1422         {
1423                 throw new NotImplementedException ();
1424         }
1425
1426 #if NET_2_0
1427         [Obsolete]
1428 #endif
1429         [EditorBrowsable (EditorBrowsableState.Advanced)]
1430         public void RegisterOnSubmitStatement (string key, string script)
1431         {
1432                 scriptManager.RegisterOnSubmitStatement (key, script);
1433         }
1434
1435         internal string GetSubmitStatements ()
1436         {
1437                 return scriptManager.WriteSubmitStatements ();
1438         }
1439
1440         [EditorBrowsable (EditorBrowsableState.Advanced)]
1441         public void RegisterRequiresPostBack (Control control)
1442         {
1443                 if (_requiresPostBack == null)
1444                         _requiresPostBack = new ArrayList ();
1445
1446                 if (_requiresPostBack.Contains (control.UniqueID))
1447                         return;
1448
1449                 _requiresPostBack.Add (control.UniqueID);
1450         }
1451
1452         [EditorBrowsable (EditorBrowsableState.Advanced)]
1453         public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
1454         {
1455                 requiresRaiseEvent = control;
1456         }
1457
1458 #if NET_2_0
1459         [Obsolete]
1460 #endif
1461         [EditorBrowsable (EditorBrowsableState.Advanced)]
1462         public virtual void RegisterStartupScript (string key, string script)
1463         {
1464                 scriptManager.RegisterStartupScript (key, script);
1465         }
1466
1467         [EditorBrowsable (EditorBrowsableState.Advanced)]
1468         public void RegisterViewStateHandler ()
1469         {
1470                 handleViewState = true;
1471         }
1472
1473         [EditorBrowsable (EditorBrowsableState.Advanced)]
1474         protected virtual void SavePageStateToPersistenceMedium (object viewState)
1475         {
1476                 PageStatePersister persister = this.PageStatePersister;
1477                 if (persister == null)
1478                         return;
1479                 Pair pair = viewState as Pair;
1480                 if (pair != null) {
1481                         persister.ViewState = pair.First;
1482                         persister.ControlState = pair.Second;
1483                 } else
1484                         persister.ViewState = viewState;
1485                 persister.Save ();
1486         }
1487
1488         internal string RawViewState {
1489                 get {
1490                         NameValueCollection postdata = _requestValueCollection;
1491                         string view_state;
1492                         if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
1493                                 return null;
1494
1495                         if (view_state == "")
1496                                 return null;
1497                         return view_state;
1498                 }
1499                 set { _savedViewState = value; }
1500         }
1501
1502 #if NET_2_0
1503         protected virtual 
1504 #else
1505         internal
1506 #endif
1507         PageStatePersister PageStatePersister {
1508                 get {
1509                         if (page_state_persister == null)
1510                                 page_state_persister = new HiddenFieldPageStatePersister (this);
1511                         return page_state_persister;
1512                 }
1513         }
1514         
1515         [EditorBrowsable (EditorBrowsableState.Advanced)]
1516         protected virtual object LoadPageStateFromPersistenceMedium ()
1517         {
1518                 PageStatePersister persister = this.PageStatePersister;
1519                 if (persister == null)
1520                         return null;
1521                 persister.Load ();
1522                 return new Pair (persister.ViewState, persister.ControlState);
1523         }
1524
1525         internal void LoadPageViewState()
1526         {
1527                 Pair sState = LoadPageStateFromPersistenceMedium () as Pair;
1528                 if (sState != null) {
1529                         if (allow_load) {
1530 #if NET_2_0
1531                                 LoadPageControlState (sState.Second);
1532 #endif
1533                                 Pair vsr = sState.First as Pair;
1534                                 if (vsr != null) {
1535                                         LoadViewStateRecursive (vsr.First);
1536                                         _requiresPostBackCopy = vsr.Second as ArrayList;
1537                                 }
1538                         }
1539                 }
1540         }
1541
1542         internal void SavePageViewState ()
1543         {
1544                 if (!handleViewState)
1545                         return;
1546
1547 #if NET_2_0
1548                 object controlState = SavePageControlState ();
1549 #endif
1550
1551                 object viewState = SaveViewStateRecursive ();
1552                 object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
1553                 Pair vsr = null;
1554
1555                 if (viewState != null || reqPostback != null)
1556                         vsr = new Pair (viewState, reqPostback);
1557                 Pair pair = new Pair ();
1558
1559                 pair.First = vsr;
1560 #if NET_2_0
1561                 pair.Second = controlState;
1562 #else
1563                 pair.Second = null;
1564 #endif
1565                 if (pair.First == null && pair.Second == null)
1566                         SavePageStateToPersistenceMedium (null);
1567                 else
1568                         SavePageStateToPersistenceMedium (pair);                
1569
1570         }
1571
1572         public virtual void Validate ()
1573         {
1574                 is_validated = true;
1575                 ValidateCollection (_validators);
1576         }
1577
1578 #if NET_2_0
1579         internal bool AreValidatorsUplevel () {
1580                 return AreValidatorsUplevel (String.Empty);
1581         }
1582
1583         internal bool AreValidatorsUplevel (string valGroup)
1584 #else
1585         internal virtual bool AreValidatorsUplevel ()
1586 #endif
1587         {
1588                 bool uplevel = false;
1589
1590                 foreach (IValidator v in Validators) {
1591                         BaseValidator bv = v as BaseValidator;
1592                         if (bv == null) continue;
1593
1594 #if NET_2_0
1595                         if (valGroup != bv.ValidationGroup)
1596                                 continue;
1597 #endif
1598                         if (bv.GetRenderUplevel()) {
1599                                 uplevel = true;
1600                                 break;
1601                         }
1602                 }
1603
1604                 return uplevel;
1605         }
1606
1607         bool ValidateCollection (ValidatorCollection validators)
1608         {
1609 #if NET_2_0
1610                 if (!_eventValidation)
1611                         return true;
1612 #endif
1613
1614                 if (validators == null || validators.Count == 0)
1615                         return true;
1616
1617                 bool all_valid = true;
1618                 foreach (IValidator v in validators){
1619                         v.Validate ();
1620                         if (v.IsValid == false)
1621                                 all_valid = false;
1622                 }
1623
1624                 return all_valid;
1625         }
1626
1627         [EditorBrowsable (EditorBrowsableState.Advanced)]
1628         public virtual void VerifyRenderingInServerForm (Control control)
1629         {
1630                 if (_context == null)
1631                         return;
1632
1633                 if (!renderingForm)
1634                         throw new HttpException ("Control '" +
1635                                                  control.ClientID +
1636                                                  "' of type '" +
1637                                                  control.GetType ().Name +
1638                                                  "' must be placed inside a form tag with runat=server.");
1639         }
1640
1641         protected override void FrameworkInitialize ()
1642         {
1643                 base.FrameworkInitialize ();
1644 #if NET_2_0
1645                 InitializeStyleSheet ();
1646 #endif
1647         }
1648
1649         #endregion
1650         
1651         #if NET_2_0
1652         public
1653         #else
1654         internal
1655         #endif
1656                 ClientScriptManager ClientScript {
1657                 get { return scriptManager; }
1658         }
1659         
1660         #if NET_2_0
1661         
1662         static readonly object InitCompleteEvent = new object ();
1663         static readonly object LoadCompleteEvent = new object ();
1664         static readonly object PreInitEvent = new object ();
1665         static readonly object PreLoadEvent = new object ();
1666         static readonly object PreRenderCompleteEvent = new object ();
1667         static readonly object SaveStateCompleteEvent = new object ();
1668         int event_mask;
1669         const int initcomplete_mask = 1;
1670         const int loadcomplete_mask = 1 << 1;
1671         const int preinit_mask = 1 << 2;
1672         const int preload_mask = 1 << 3;
1673         const int prerendercomplete_mask = 1 << 4;
1674         const int savestatecomplete_mask = 1 << 5;
1675         
1676         [EditorBrowsable (EditorBrowsableState.Advanced)]
1677         public event EventHandler InitComplete {
1678                 add {
1679                         event_mask |= initcomplete_mask;
1680                         Events.AddHandler (InitCompleteEvent, value);
1681                 }
1682                 remove { Events.RemoveHandler (InitCompleteEvent, value); }
1683         }
1684         
1685         [EditorBrowsable (EditorBrowsableState.Advanced)]
1686         public event EventHandler LoadComplete {
1687                 add {
1688                         event_mask |= loadcomplete_mask;
1689                         Events.AddHandler (LoadCompleteEvent, value);
1690                 }
1691                 remove { Events.RemoveHandler (LoadCompleteEvent, value); }
1692         }
1693         
1694         public event EventHandler PreInit {
1695                 add {
1696                         event_mask |= preinit_mask;
1697                         Events.AddHandler (PreInitEvent, value);
1698                 }
1699                 remove { Events.RemoveHandler (PreInitEvent, value); }
1700         }
1701         
1702         [EditorBrowsable (EditorBrowsableState.Advanced)]
1703         public event EventHandler PreLoad {
1704                 add {
1705                         event_mask |= preload_mask;
1706                         Events.AddHandler (PreLoadEvent, value);
1707                 }
1708                 remove { Events.RemoveHandler (PreLoadEvent, value); }
1709         }
1710         
1711         [EditorBrowsable (EditorBrowsableState.Advanced)]
1712         public event EventHandler PreRenderComplete {
1713                 add {
1714                         event_mask |= prerendercomplete_mask;
1715                         Events.AddHandler (PreRenderCompleteEvent, value);
1716                 }
1717                 remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
1718         }
1719         
1720         [EditorBrowsable (EditorBrowsableState.Advanced)]
1721         public event EventHandler SaveStateComplete {
1722                 add {
1723                         event_mask |= savestatecomplete_mask;
1724                         Events.AddHandler (SaveStateCompleteEvent, value);
1725                 }
1726                 remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
1727         }
1728         
1729         protected virtual void OnInitComplete (EventArgs e)
1730         {
1731                 if ((event_mask & initcomplete_mask) != 0) {
1732                         EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
1733                         if (eh != null) eh (this, e);
1734                 }
1735         }
1736         
1737         protected virtual void OnLoadComplete (EventArgs e)
1738         {
1739                 if ((event_mask & loadcomplete_mask) != 0) {
1740                         EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
1741                         if (eh != null) eh (this, e);
1742                 }
1743         }
1744         
1745         protected virtual void OnPreInit (EventArgs e)
1746         {
1747                 if ((event_mask & preinit_mask) != 0) {
1748                         EventHandler eh = (EventHandler) (Events [PreInitEvent]);
1749                         if (eh != null) eh (this, e);
1750                 }
1751         }
1752         
1753         protected virtual void OnPreLoad (EventArgs e)
1754         {
1755                 if ((event_mask & preload_mask) != 0) {
1756                         EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
1757                         if (eh != null) eh (this, e);
1758                 }
1759         }
1760         
1761         protected virtual void OnPreRenderComplete (EventArgs e)
1762         {
1763                 if ((event_mask & prerendercomplete_mask) != 0) {
1764                         EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
1765                         if (eh != null) eh (this, e);
1766                 }
1767
1768                 if (Form == null)
1769                         return;
1770                 if (!Form.DetermineRenderUplevel ())
1771                         return;
1772
1773                 /* figure out if we have some control we're going to focus */
1774                 if (String.IsNullOrEmpty (_focusedControlID)) {
1775                         _focusedControlID = Form.DefaultFocus;
1776                         if (String.IsNullOrEmpty (_focusedControlID))
1777                                 _focusedControlID = Form.DefaultButton;
1778                 }
1779
1780                 if (!String.IsNullOrEmpty (_focusedControlID) || Form.SubmitDisabledControls) {
1781
1782                         RequiresPostBackScript ();
1783                         ClientScript.RegisterWebFormClientScript ();
1784
1785                         if (!String.IsNullOrEmpty (_focusedControlID)) {
1786                                 ClientScript.RegisterStartupScript ("HtmlForm-DefaultButton-StartupScript",
1787                                                                          String.Format ("<script type=\"text/javascript\">\n" +
1788                                                                                         "<!--\n" +
1789                                                                                         "WebForm_AutoFocus('{0}');// -->\n" +
1790                                                                                         "</script>\n", _focusedControlID));
1791                         }
1792
1793                         if (Form.SubmitDisabledControls) {
1794                                 ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
1795                                                                                  "javascript: return WebForm_OnSubmit();");
1796                                 ClientScript.RegisterStartupScript ("HtmlForm-SubmitDisabledControls-StartupScript",
1797 @"<script language=""JavaScript"">
1798 <!--
1799 function WebForm_OnSubmit() {
1800 WebForm_ReEnableControls();
1801 return true;
1802 } // -->
1803 </script>");
1804                         }
1805                 }
1806         }
1807         
1808         protected virtual void OnSaveStateComplete (EventArgs e)
1809         {
1810                 if ((event_mask & savestatecomplete_mask) != 0) {
1811                         EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
1812                         if (eh != null) eh (this, e);
1813                 }
1814         }
1815         
1816         public HtmlForm Form {
1817                 get { return _form; }
1818         }
1819         
1820         internal void RegisterForm (HtmlForm form)
1821         {
1822                 _form = form;
1823         }
1824         
1825         [BrowsableAttribute (false)]
1826         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1827         public Page PreviousPage {
1828                 get {
1829                         if (_doLoadPreviousPage) {
1830                                 _doLoadPreviousPage = false;
1831                                 LoadPreviousPageReference ();
1832                         }
1833                         return previousPage;
1834                 }
1835         }
1836
1837         
1838         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1839         [BrowsableAttribute (false)]
1840         public bool IsCallback {
1841                 get { return isCallback; }
1842         }
1843         
1844         [BrowsableAttribute (false)]
1845         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1846         public bool IsCrossPagePostBack {
1847                 get { return isCrossPagePostBack; }
1848         }
1849
1850         public new virtual char IdSeparator {
1851                 get {
1852                         //TODO: why override?
1853                         return base.IdSeparator;
1854                 }
1855         }
1856         
1857         string ProcessCallbackData ()
1858         {
1859                 string callbackTarget = _requestValueCollection [CallbackSourceID];
1860                 if (callbackTarget == null || callbackTarget.Length == 0)
1861                         throw new HttpException ("Callback target not provided.");
1862
1863                 Control targetControl = FindControl (callbackTarget);
1864                 ICallbackEventHandler target = targetControl as ICallbackEventHandler;
1865                 if (target == null)
1866                         throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
1867
1868                 string callbackArgument = _requestValueCollection [CallbackArgumentID];
1869                 target.RaiseCallbackEvent (callbackArgument);
1870                 return target.GetCallbackResult ();
1871         }
1872
1873         [BrowsableAttribute (false)]
1874         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1875         public HtmlHead Header {
1876                 get { return htmlHeader; }
1877         }
1878         
1879         internal void SetHeader (HtmlHead header)
1880         {
1881                 htmlHeader = header;
1882                 if (_title != null) {
1883                         htmlHeader.Title = _title;
1884                         _title = null;
1885                 }
1886         }
1887
1888         [MonoTODO("Not Implemented")]
1889         protected bool AsyncMode {
1890                 get {
1891                         throw new NotImplementedException ();
1892                 }
1893                 set {
1894                         throw new NotImplementedException ();
1895                 }
1896         }
1897
1898         [MonoTODO ("Not Implemented")]
1899         public TimeSpan AsyncTimeout {
1900                 get {
1901                         throw new NotImplementedException ();
1902                 }
1903                 set {
1904                         throw new NotImplementedException ();
1905                 }
1906         }
1907
1908         [MonoTODO ("Not Implemented")]
1909         public bool IsAsync {
1910                 get {
1911                         throw new NotImplementedException ();
1912                 }
1913         }
1914         
1915         [MonoTODO ("Not Implemented")]
1916         protected internal virtual string UniqueFilePathSuffix {
1917                 get {
1918                         throw new NotImplementedException ();
1919                 }
1920         }
1921
1922         [MonoTODO ("Not Implemented")]
1923         public int MaxPageStateFieldLength {
1924                 get {
1925                         throw new NotImplementedException ();
1926                 }
1927                 set {
1928                         throw new NotImplementedException ();
1929                 }
1930         }
1931
1932         [MonoTODO ("Not Implemented")]
1933         public ViewStateEncryptionMode ViewStateEncryptionMode {
1934                 get {
1935                         throw new NotImplementedException ();
1936                 }
1937                 set {
1938                         throw new NotImplementedException ();
1939                 }
1940         }
1941
1942         [MonoTODO ("Not Implemented")]
1943         public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler)
1944         {
1945                 throw new NotImplementedException ();
1946         }
1947
1948         [MonoTODO ("Not Implemented")]
1949         public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler, Object state)
1950         {
1951                 throw new NotImplementedException ();
1952         }
1953
1954         [MonoTODO ("Not Implemented")]
1955         public void ExecuteRegisteredAsyncTasks ()
1956         {
1957                 throw new NotImplementedException ();
1958         }
1959
1960         [MonoTODO ("Not Implemented")]
1961         public static HtmlTextWriter CreateHtmlTextWriterFromType (TextWriter tw, Type writerType)
1962         {
1963                 throw new NotImplementedException ();
1964         }
1965
1966         [MonoTODO ("Not Implemented")]
1967         public void RegisterRequiresViewStateEncryption ()
1968         {
1969                 throw new NotImplementedException ();
1970         }
1971
1972         void ApplyMasterPage ()
1973         {
1974                 if (masterPageFile != null && masterPageFile.Length > 0) {
1975                         ArrayList appliedMasterPageFiles = new ArrayList ();
1976
1977                         if (Master != null) {
1978                                 MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
1979
1980                                 Master.Page = this;
1981                                 Controls.Clear ();
1982                                 Controls.Add (Master);
1983                         }
1984                 }
1985         }
1986
1987         [DefaultValueAttribute ("")]
1988         public virtual string MasterPageFile {
1989                 get { return masterPageFile; }
1990                 set {
1991                         masterPageFile = value;
1992                         masterPage = null;
1993                 }
1994         }
1995         
1996         [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
1997         [BrowsableAttribute (false)]
1998         public MasterPage Master {
1999                 get {
2000                         if (Context == null || String.IsNullOrEmpty (masterPageFile))
2001                                 return null;
2002
2003                         if (masterPage == null)
2004                                 masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
2005
2006                         return masterPage;
2007                 }
2008         }
2009         
2010         public void SetFocus (string clientID)
2011         {
2012                 if (String.IsNullOrEmpty (clientID))
2013                         throw new ArgumentNullException ("control");
2014
2015                 if (_lifeCycle > PageLifeCycle.PreRender)
2016                         throw new InvalidOperationException ("SetFocus can only be called before and during PreRender.");
2017
2018                 if(Form==null)
2019                         throw new InvalidOperationException ("A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property.");
2020
2021                 _focusedControlID = clientID;
2022         }
2023
2024         public void SetFocus (Control control)
2025         {
2026                 if (control == null)
2027                         throw new ArgumentNullException ("control");
2028
2029                 SetFocus (control.ClientID);
2030         }
2031         
2032         [EditorBrowsable (EditorBrowsableState.Advanced)]
2033         public void RegisterRequiresControlState (Control control)
2034         {
2035                 if (control == null)
2036                         throw new ArgumentNullException ("control");
2037
2038                 if (RequiresControlState (control))
2039                         return;
2040
2041                 if (requireStateControls == null)
2042                         requireStateControls = new ArrayList ();
2043                 int n = requireStateControls.Add (control);
2044
2045                 if (_savedControlState == null || n >= _savedControlState.Length) 
2046                         return;
2047
2048                 for (Control parent = control.Parent; parent != null; parent = parent.Parent)
2049                         if (parent.IsChildControlStateCleared)
2050                                 return;
2051
2052                 object state = _savedControlState [n];
2053                 if (state != null)
2054                         control.LoadControlState (state);
2055         }
2056         
2057         public bool RequiresControlState (Control control)
2058         {
2059                 if (requireStateControls == null) return false;
2060                 return requireStateControls.Contains (control);
2061         }
2062         
2063         [EditorBrowsable (EditorBrowsableState.Advanced)]
2064         public void UnregisterRequiresControlState (Control control)
2065         {
2066                 if (requireStateControls != null)
2067                         requireStateControls.Remove (control);
2068         }
2069         
2070         public ValidatorCollection GetValidators (string validationGroup)
2071         {
2072                 string valgr = validationGroup;
2073                 if (valgr == null)
2074                         valgr = String.Empty;
2075
2076                 if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
2077                 ValidatorCollection col = _validatorsByGroup [valgr] as ValidatorCollection;
2078                 if (col == null) {
2079                         col = new ValidatorCollection ();
2080                         _validatorsByGroup [valgr] = col;
2081                 }
2082                 return col;
2083         }
2084         
2085         public virtual void Validate (string validationGroup)
2086         {
2087                 is_validated = true;
2088                 if (validationGroup == null)
2089                         ValidateCollection (_validatorsByGroup [String.Empty] as ValidatorCollection);
2090                 else if (_validatorsByGroup != null) {
2091                         ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
2092                 }
2093         }
2094
2095         object SavePageControlState ()
2096         {
2097                 if (requireStateControls == null) return null;
2098                 object[] state = new object [requireStateControls.Count];
2099                 
2100                 bool allNull = true;
2101                 for (int n=0; n<state.Length; n++) {
2102                         state [n] = ((Control) requireStateControls [n]).SaveControlState ();
2103                         if (state [n] != null) allNull = false;
2104                 }
2105                 if (allNull) return null;
2106                 else return state;
2107         }
2108         
2109         void LoadPageControlState (object data)
2110         {
2111                 _savedControlState = (object []) data;
2112                 
2113                 if (requireStateControls == null) return;
2114
2115                 int max = Math.Min (requireStateControls.Count, _savedControlState != null ? _savedControlState.Length : requireStateControls.Count);
2116                 for (int n=0; n < max; n++) {
2117                         Control ctl = (Control) requireStateControls [n];
2118                         ctl.LoadControlState (_savedControlState != null ? _savedControlState [n] : null);
2119                 }
2120         }
2121
2122         void LoadPreviousPageReference ()
2123         {
2124                 if (_requestValueCollection != null) {
2125                         string prevPage = _requestValueCollection [PreviousPageID];
2126                         if (prevPage != null) {
2127                                 previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
2128                                 previousPage.ProcessCrossPagePostBack (_context);
2129                         } 
2130                 }
2131         }
2132
2133
2134         Hashtable contentTemplates;
2135         [EditorBrowsable (EditorBrowsableState.Never)]
2136         protected internal void AddContentTemplate (string templateName, ITemplate template)
2137         {
2138                 if (contentTemplates == null)
2139                         contentTemplates = new Hashtable ();
2140                 contentTemplates [templateName] = template;
2141         }
2142
2143         PageTheme _pageTheme;
2144         internal PageTheme PageTheme {
2145                 get { return _pageTheme; }
2146         }
2147
2148         PageTheme _styleSheetPageTheme;
2149         internal PageTheme StyleSheetPageTheme {
2150                 get { return _styleSheetPageTheme; }
2151         }
2152
2153         Stack dataItemCtx;
2154         
2155         internal void PushDataItemContext (object o) {
2156                 if (dataItemCtx == null)
2157                         dataItemCtx = new Stack ();
2158                 
2159                 dataItemCtx.Push (o);
2160         }
2161         
2162         internal void PopDataItemContext () {
2163                 if (dataItemCtx == null)
2164                         throw new InvalidOperationException ();
2165                 
2166                 dataItemCtx.Pop ();
2167         }
2168         
2169         public object GetDataItem() {
2170                 if (dataItemCtx == null || dataItemCtx.Count == 0)
2171                         throw new InvalidOperationException ("No data item");
2172                 
2173                 return dataItemCtx.Peek ();
2174         }
2175
2176         protected internal override void OnInit (EventArgs e)
2177         {
2178                 base.OnInit (e);
2179                 if (Header == null)
2180                         return;
2181
2182                 ArrayList themes = new ArrayList();
2183
2184                 if (StyleSheetPageTheme != null && StyleSheetPageTheme.GetStyleSheets () != null)
2185                         themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
2186                 if (PageTheme != null && PageTheme.GetStyleSheets () != null)
2187                         themes.AddRange (PageTheme.GetStyleSheets ());
2188
2189                 foreach (string lss in themes) {
2190                         HtmlLink hl = new HtmlLink ();
2191                         hl.Href = ResolveUrl (lss);
2192                         hl.Attributes["type"] = "text/css";
2193                         hl.Attributes["rel"] = "stylesheet";
2194                         Header.Controls.Add (hl);
2195                 }
2196         }
2197
2198         #endif
2199
2200 #if NET_2_0
2201         [MonoTODO ("Not implemented.  Only used by .net aspx parser")]
2202         protected object GetWrappedFileDependencies (string [] list)
2203         {
2204                 return null;
2205         }
2206
2207         [MonoTODO ("Does nothing.  Used by .net aspx parser")]
2208         protected virtual void InitializeCulture ()
2209         {
2210         }
2211
2212         [MonoTODO ("Does nothing. Used by .net aspx parser")]
2213         protected internal void AddWrappedFileDependencies (object virtualFileDependencies)
2214         {
2215         }
2216 #endif
2217 }
2218 }