* CssStyleCollection.cs: used ListDictionary instead of HybridDictionary as underlayi...
[mono.git] / mcs / class / System.Web / System.Web.UI / Control.cs
1 //
2 // System.Web.UI.Control.cs
3 //
4 // Authors:
5 //   Bob Smith <bob@thestuff.net>
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //   Sanjay Gupta (gsanjay@novell.com)
9 //
10 // (C) Bob Smith
11 // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
12 // (C) 2004 Novell, Inc. (http://www.novell.com)
13 //
14
15 //
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 //
35
36 // This will provide extra information when trace is enabled. Might be too verbose.
37 #define MONO_TRACE
38
39 using System.Collections;
40 using System.ComponentModel;
41 using System.ComponentModel.Design;
42 using System.ComponentModel.Design.Serialization;
43 using System.Security.Permissions;
44 using System.Web;
45 using System.Web.Util;
46 using System.Globalization;
47 #if NET_2_0
48 using System.Web.UI.Adapters;
49 using System.IO;
50 #endif
51
52 namespace System.Web.UI
53 {
54         // CAS
55         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
56         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
57         // attributes
58         [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
59         [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
60         [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
61 #if NET_2_0
62         [DesignerSerializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VisualStudio_Web,
63                                 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
64         [Bindable (true)]
65         [Themeable (false)]
66 #else
67         [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
68                                 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
69 #endif
70         public partial class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
71 #if NET_2_0
72 , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
73 #endif
74         {
75                 static readonly object DataBindingEvent = new object ();
76                 static readonly object DisposedEvent = new object ();
77                 static readonly object InitEvent = new object ();
78                 static readonly object LoadEvent = new object ();
79                 static readonly object PreRenderEvent = new object ();
80                 static readonly object UnloadEvent = new object ();
81                 static string [] defaultNameArray;
82                 /* */
83                 int event_mask;
84                 const int databinding_mask = 1;
85                 const int disposed_mask = 1 << 1;
86                 const int init_mask = 1 << 2;
87                 const int load_mask = 1 << 3;
88                 const int prerender_mask = 1 << 4;
89                 const int unload_mask = 1 << 5;
90                 /* */
91
92                 string uniqueID;
93                 string _userId;
94                 ControlCollection _controls;
95                 Control _namingContainer;
96                 Page _page;
97                 Control _parent;
98                 ISite _site;
99                 HttpContext _context;
100                 StateBag _viewState;
101                 EventHandlerList _events;
102                 RenderMethod _renderMethodDelegate;
103                 Hashtable _controlsCache;
104                 int defaultNumberID;
105
106                 DataBindingCollection dataBindings;
107                 Hashtable pendingVS; // may hold unused viewstate data from child controls
108
109
110 #if NET_2_0
111                 TemplateControl _templateControl;
112                 bool _isChildControlStateCleared;
113 #endif
114                 string _templateSourceDirectory;
115
116                 /*************/
117                 int stateMask;
118                 const int ENABLE_VIEWSTATE = 1;
119                 const int VISIBLE = 1 << 1;
120                 const int AUTOID = 1 << 2;
121                 const int CREATING_CONTROLS = 1 << 3;
122                 const int BINDING_CONTAINER = 1 << 4;
123                 const int AUTO_EVENT_WIREUP = 1 << 5;
124                 const int IS_NAMING_CONTAINER = 1 << 6;
125                 const int VISIBLE_CHANGED = 1 << 7;
126                 const int TRACK_VIEWSTATE = 1 << 8;
127                 const int CHILD_CONTROLS_CREATED = 1 << 9;
128                 const int ID_SET = 1 << 10;
129                 const int INITED = 1 << 11;
130                 const int INITING = 1 << 12;
131                 const int VIEWSTATE_LOADED = 1 << 13;
132                 const int LOADED = 1 << 14;
133                 const int PRERENDERED = 1 << 15;
134 #if NET_2_0
135                 const int ENABLE_THEMING = 1 << 16;
136 #endif
137                 const int AUTOID_SET = 1 << 17;
138                 /*************/
139
140                 static Control () {
141                         defaultNameArray = new string [100];
142                         for (int i = 0; i < 100; i++)
143 #if NET_2_0
144                                 defaultNameArray [i] = String.Format ("ctl{0:D2}", i);
145 #else
146                                 defaultNameArray [i] = "_ctl" + i;
147 #endif
148                 }
149
150                 public Control () {
151                         stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
152                         if (this is INamingContainer)
153                                 stateMask |= IS_NAMING_CONTAINER;
154                 }
155
156 #if NET_2_0
157                 [MonoTODO ("Not implemented, always returns null")]
158                 protected ControlAdapter Adapter {
159                         get {
160                                 // for the time being, fool the
161                                 // Control machinery into thinking we
162                                 // don't have an Adapter.  This will
163                                 // allow us to write all the rest of
164                                 // the Adapter handling code without
165                                 // having to worry about *having*
166                                 // adapters.
167                                 return null;
168                         }
169                 }
170
171                 string _appRelativeTemplateSourceDirectory = null;
172
173                 [EditorBrowsable (EditorBrowsableState.Advanced)]
174                 [Browsable (false)]
175                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
176                 public string AppRelativeTemplateSourceDirectory {
177                         get {
178                                 if (_appRelativeTemplateSourceDirectory != null)
179                                         return _appRelativeTemplateSourceDirectory;
180
181                                 string tempSrcDir = null;
182
183                                 TemplateControl templateControl = TemplateControl;
184                                 if (templateControl != null)
185                                         if (!string.IsNullOrEmpty (templateControl.AppRelativeVirtualPath))
186                                                 tempSrcDir = VirtualPathUtility.GetDirectory (templateControl.AppRelativeVirtualPath, false);
187
188                                 _appRelativeTemplateSourceDirectory = (tempSrcDir != null) ? tempSrcDir : "~/";
189                                 return _appRelativeTemplateSourceDirectory;
190                         }
191                         [EditorBrowsable (EditorBrowsableState.Never)]
192                         set {
193                                 _appRelativeTemplateSourceDirectory = value;
194                                 _templateSourceDirectory = null;
195                         }
196                 }
197
198 #endif
199
200                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
201                 [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
202                 public Control BindingContainer {
203                         get {
204                                 Control container = NamingContainer;
205                                 if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
206                                         container = container.BindingContainer;
207                                 return container;
208                         }
209                 }
210
211                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
212                 [Browsable (false)]
213                 [WebSysDescription ("An Identification of the control that is rendered.")]
214                 public virtual string ClientID {
215                         get {
216                                 string client = UniqueID;
217
218                                 if (client != null)
219 #if NET_2_0
220                                         client = UniqueID2ClientID (client);
221 #else
222                                         client = client.Replace (':', ClientIDSeparator);
223 #endif
224
225                                 stateMask |= ID_SET;
226                                 return client;
227                         }
228                 }
229
230 #if NET_2_0
231                 internal string UniqueID2ClientID (string uniqueId) {
232                         return uniqueId.Replace (IdSeparator, ClientIDSeparator);
233                 }
234
235                 protected char ClientIDSeparator
236 #else
237                 char ClientIDSeparator
238 #endif
239  {
240                         get {
241                                 return '_';
242                         }
243                 }
244
245
246                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
247                 [Browsable (false)]
248                 [WebSysDescription ("The child controls of this control.")]
249                 public virtual ControlCollection Controls //DIT
250                 {
251                         get {
252                                 if (_controls == null)
253                                         _controls = CreateControlCollection ();
254                                 return _controls;
255                         }
256                 }
257
258 #if NET_2_0
259                 [MonoTODO ("revisit once we have a real design strategy")]
260                 protected internal bool DesignMode {
261                         get { return false; }
262                 }
263 #endif
264
265                 [DefaultValue (true), WebCategory ("Behavior")]
266                 [WebSysDescription ("An Identification of the control that is rendered.")]
267 #if NET_2_0
268                 [Themeable (false)]
269 #endif
270                 public virtual bool EnableViewState {
271                         get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
272                         set { SetMask (ENABLE_VIEWSTATE, value); }
273                 }
274
275                 [MergableProperty (false), ParenthesizePropertyName (true)]
276                 [WebSysDescription ("The name of the control that is rendered.")]
277 #if NET_2_0
278                 [Filterable (false), Themeable (false)]
279 #endif
280
281                 public virtual string ID {
282                         get {
283                                 return (((stateMask & ID_SET) != 0) ? _userId : null);
284                         }
285
286                         set {
287                                 if (value == "")
288                                         value = null;
289
290                                 stateMask |= ID_SET;
291                                 _userId = value;
292                                 NullifyUniqueID ();
293                         }
294                 }
295
296 #if NET_2_0
297                 protected char IdSeparator {
298                         get {
299                                 return '$';
300                         }
301                 }
302
303                 protected internal bool IsChildControlStateCleared {
304                         get { return _isChildControlStateCleared; }
305                 }
306
307                 protected internal bool IsViewStateEnabled {
308                         get {
309
310                                 for (Control control = this; control != null; control = control.Parent)
311                                         if (!control.EnableViewState)
312                                                 return false;
313
314                                 return true;
315                         }
316                 }
317
318                 protected bool LoadViewStateByID {
319                         get {
320                                 return false;
321                         }
322                 }
323 #endif
324
325                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
326                 [Browsable (false)]
327                 [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
328                 public virtual Control NamingContainer {
329                         get {
330                                 if (_namingContainer == null && _parent != null) {
331                                         if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
332                                                 _namingContainer = _parent.NamingContainer;
333                                         else
334                                                 _namingContainer = _parent;
335                                 }
336
337                                 return _namingContainer;
338                         }
339                 }
340
341                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
342                 [Browsable (false)]
343                 [WebSysDescription ("The webpage that this control resides on.")]
344 #if NET_2_0
345                 [Bindable (false)]
346 #endif
347                 public virtual Page Page //DIT
348                                 {
349                         get {
350                                 if (_page == null){
351                                         if (NamingContainer != null)
352                                                 _page = NamingContainer.Page;
353                                         else if (Parent != null)
354                                                 _page = Parent.Page;
355                                 }
356                                 return _page;
357                         }
358                         set {
359                                 _page = value;
360                         }
361                 }
362
363                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
364                 [Browsable (false)]
365                 [WebSysDescription ("The parent control of this control.")]
366                 public virtual Control Parent //DIT
367                 {
368                         get {
369                                 return _parent;
370                         }
371                 }
372
373                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
374                 [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
375                 [WebSysDescription ("The site this control is part of.")]
376                 public ISite Site //DIT
377                 {
378                         get {
379                                 return _site;
380                         }
381                         set {
382                                 _site = value;
383                         }
384                 }
385
386 #if NET_2_0
387                 [Browsable (false)]
388                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
389                 public TemplateControl TemplateControl {
390                         get {
391                                 return TemplateControlInternal;
392                         }
393
394                         [EditorBrowsable (EditorBrowsableState.Never)]
395                         set { _templateControl = value; }
396                 }
397
398                 internal virtual TemplateControl TemplateControlInternal {
399                         get {
400                                 if (_templateControl != null)
401                                         return _templateControl;
402                                 if (_parent != null)
403                                         return _parent.TemplateControl;
404                                 return null;
405                         }
406                 }
407 #endif
408
409 #if !TARGET_J2EE
410                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
411                 [Browsable (false)]
412                 [WebSysDescription ("A virtual directory containing the parent of the control.")]
413                 public virtual string TemplateSourceDirectory {
414                         get {
415 #if NET_2_0
416                                 if (_templateSourceDirectory == null) {
417                                         TemplateControl tc = TemplateControl;
418
419                                         if (tc == null) {
420                                                 HttpContext ctx = Context;
421                                                 if (ctx != null)
422                                                         _templateSourceDirectory = VirtualPathUtility.GetDirectory (ctx.Request.CurrentExecutionFilePath);
423                                         }
424                                         else if (tc != this)
425                                                 _templateSourceDirectory = tc.TemplateSourceDirectory;
426
427                                         if (_templateSourceDirectory == null && this is TemplateControl) {
428                                                 string path = ((TemplateControl) this).AppRelativeVirtualPath;
429
430                                                 if (path != null) {
431                                                         // Pretend our application virtual root is "/" even if it isn't - we just
432                                                         // want to get an absolute url out of relative one, without the real
433                                                         // application root prepended to it.
434                                                         string ret = VirtualPathUtility.GetDirectory (VirtualPathUtility.ToAbsolute (path, "/"));
435                                                         int len = ret.Length;
436                                                         if (len <= 1)
437                                                                 return ret;
438                                                         if (ret [--len] == '/')
439                                                                 _templateSourceDirectory = ret.Substring (0, len);
440                                                 }
441                                                 else
442                                                         _templateSourceDirectory = String.Empty;
443                                         }
444                                         if (_templateSourceDirectory == null)
445                                                 _templateSourceDirectory = String.Empty;
446                                 }
447
448                                 return _templateSourceDirectory;
449 #else
450                                 if (_templateSourceDirectory == null) {
451                                         HttpContext ctx = HttpContext.Current;
452                                         HttpRequest req = ctx != null ? ctx.Request : null;
453                                         
454                                         _templateSourceDirectory = (_parent == null)
455                                                 ? req != null ? VirtualPathUtility.RemoveTrailingSlash (
456                                                         VirtualPathUtility.GetDirectory (
457                                                                 HttpContext.Current.Request.CurrentExecutionFilePath)) : String.Empty
458                                                 : _parent.TemplateSourceDirectory;
459                                 }
460                                 return _templateSourceDirectory;
461 #endif
462                         }
463                 }
464 #endif
465
466                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
467                 [Browsable (false)]
468                 [WebSysDescription ("The unique ID of the control.")]
469                 public virtual string UniqueID {
470                         get {
471                                 if (uniqueID != null)
472                                         return uniqueID;
473
474                                 if (NamingContainer == null)
475                                         return _userId;
476
477                                 EnsureIDInternal ();
478
479                                 string prefix = NamingContainer.UniqueID;
480 #if TARGET_J2EE
481                                 // For J2EE portlets we need to add the namespace to the ID.
482                                 if (NamingContainer == Page && Page.PortletNamespace != null)
483                                         prefix = Page.PortletNamespace;
484                                 else
485 #endif
486                                 if (NamingContainer == Page || prefix == null) {
487                                         uniqueID = _userId;
488                                         return uniqueID;
489                                 }
490
491 #if NET_2_0
492                                 uniqueID = prefix + IdSeparator + _userId;
493 #else
494                                 uniqueID = prefix + ":" + _userId;
495 #endif
496                                 return uniqueID;
497                         }
498                 }
499
500                 void SetMask (int m, bool val) {
501                         if (val)
502                                 stateMask |= m;
503                         else
504                                 stateMask &= ~m;
505                 }
506
507                 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
508                 [WebSysDescription ("Visiblity state of the control.")]
509                 public virtual bool Visible {
510                         get {
511                                 if ((stateMask & VISIBLE) == 0)
512                                         return false;
513
514                                 if (_parent != null)
515                                         return _parent.Visible;
516
517                                 return true;
518                         }
519
520                         set {
521                                 if ((value && (stateMask & VISIBLE) == 0) ||
522                                         (!value && (stateMask & VISIBLE) != 0)) {
523                                         if (IsTrackingViewState)
524                                                 stateMask |= VISIBLE_CHANGED;
525                                 }
526
527                                 SetMask (VISIBLE, value);
528                         }
529                 }
530
531                 protected bool ChildControlsCreated {
532                         get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
533                         set {
534                                 if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
535                                         if (_controls != null)
536                                                 _controls.Clear ();
537                                 }
538
539                                 SetMask (CHILD_CONTROLS_CREATED, value);
540                         }
541                 }
542
543                 [Browsable (false)]
544                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
545                 protected virtual HttpContext Context //DIT
546                 {
547                         get {
548                                 HttpContext context;
549                                 if (_context != null)
550                                         return _context;
551                                 if (_parent == null)
552                                         return HttpContext.Current;
553                                 context = _parent.Context;
554                                 if (context != null)
555                                         return context;
556                                 return HttpContext.Current;
557                         }
558                 }
559
560                 protected EventHandlerList Events {
561                         get {
562                                 if (_events == null)
563                                         _events = new EventHandlerList ();
564                                 return _events;
565                         }
566                 }
567
568                 protected bool HasChildViewState {
569                         get {
570                                 return (pendingVS != null && pendingVS.Count > 0);
571                         }
572                 }
573
574                 protected bool IsTrackingViewState {
575                         get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
576                 }
577
578                 [Browsable (false)]
579                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
580                 [WebSysDescription ("ViewState")]
581                 protected virtual StateBag ViewState {
582                         get {
583                                 if (_viewState == null)
584                                         _viewState = new StateBag (ViewStateIgnoresCase);
585
586                                 if (IsTrackingViewState)
587                                         _viewState.TrackViewState ();
588
589                                 return _viewState;
590                         }
591                 }
592
593                 [Browsable (false)]
594                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
595                 protected virtual bool ViewStateIgnoresCase {
596                         get {
597                                 return false;
598                         }
599                 }
600
601                 internal bool AutoEventWireup {
602                         get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
603                         set { SetMask (AUTO_EVENT_WIREUP, value); }
604                 }
605
606                 internal void SetBindingContainer (bool isBC) {
607                         SetMask (BINDING_CONTAINER, isBC);
608                 }
609
610                 internal void ResetChildNames () {
611                         defaultNumberID = 0;
612                 }
613
614                 string GetDefaultName () {
615                         string defaultName;
616                         if (defaultNumberID > 99) {
617 #if NET_2_0
618                                 defaultName = "ctl" + defaultNumberID++;
619 #else
620                                 defaultName = "_ctl" + defaultNumberID++;
621 #endif
622                         }
623                         else {
624                                 defaultName = defaultNameArray [defaultNumberID++];
625                         }
626                         return defaultName;
627                 }
628
629                 void NullifyUniqueID () {
630                         uniqueID = null;
631                         if (!HasControls ())
632                                 return;
633
634                         for (int i = 0; i < _controls.Count; i++)
635                                 _controls [i].NullifyUniqueID ();
636                 }
637
638                 protected internal virtual void AddedControl (Control control, int index) {
639                         ResetControlsCache ();
640
641                         /* Ensure the control don't have more than 1 parent */
642                         if (control._parent != null)
643                                 control._parent.Controls.Remove (control);
644
645                         control._parent = this;
646                         Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
647
648                         if ((stateMask & (INITING | INITED)) != 0)
649                                 control.InitRecursive (nc);
650                         else {
651                                 control.SetNamingContainer (nc);
652                                 return;
653                         }
654
655                         if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
656                                 if (pendingVS != null) {
657                                         object vs = pendingVS [index];
658                                         if (vs != null) {
659                                                 pendingVS.Remove (index);
660                                                 if (pendingVS.Count == 0)
661                                                         pendingVS = null;
662
663                                                 control.LoadViewStateRecursive (vs);
664                                         }
665                                 }
666                         }
667
668                         if ((stateMask & LOADED) != 0)
669                                 control.LoadRecursive ();
670
671                         if ((stateMask & PRERENDERED) != 0)
672                                 control.PreRenderRecursiveInternal ();
673                 }
674                 
675                 void SetNamingContainer (Control nc) {
676                         if (nc != null) {
677                                 _namingContainer = nc;
678                                 if (AutoID)
679                                         EnsureIDInternal ();
680                         }
681                 }
682
683                 protected virtual void AddParsedSubObject (object obj) //DIT
684                 {
685                         Control c = obj as Control;
686                         if (c != null)
687                                 Controls.Add (c);
688                 }
689
690 #if NET_2_0
691                 [EditorBrowsable (EditorBrowsableState.Advanced)]
692                 public virtual void ApplyStyleSheetSkin (Page page) {
693                         if (!EnableTheming) /* this enough? */
694                                 return;
695
696                         /* apply the style sheet skin here */
697                         if (page.StyleSheetPageTheme != null) {
698                                 ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType (), SkinID);
699                                 if (cs != null)
700                                         cs.ApplySkin (this);
701                         }
702                 }
703 #endif
704
705                 protected void BuildProfileTree (string parentId, bool calcViewState) {
706                         //TODO
707                 }
708
709 #if NET_2_0
710                 protected void ClearChildControlState () {
711                         _isChildControlStateCleared = true;
712                 }
713
714                 protected void ClearChildState () {
715                         ClearChildViewState ();
716                         ClearChildControlState ();
717                 }
718 #endif
719
720                 protected void ClearChildViewState () {
721                         pendingVS = null;
722                 }
723
724 #if NET_2_0
725                 protected internal
726 #else
727                 protected
728 #endif
729  virtual void CreateChildControls () { } //DIT
730
731                 protected virtual ControlCollection CreateControlCollection () //DIT
732                 {
733                         return new ControlCollection (this);
734                 }
735
736                 protected virtual void EnsureChildControls () {
737                         if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
738                                 stateMask |= CREATING_CONTROLS;
739 #if NET_2_0
740                                 if (Adapter != null)
741                                         Adapter.CreateChildControls ();
742                                 else
743 #endif
744                                         CreateChildControls ();
745                                 ChildControlsCreated = true;
746                                 stateMask &= ~CREATING_CONTROLS;
747                         }
748                 }
749
750                 void EnsureIDInternal () {
751                         if (_userId != null)
752                                 return;
753
754                         _userId = NamingContainer.GetDefaultName ();
755                         SetMask (AUTOID_SET, true);
756                 }
757
758 #if NET_2_0
759                 protected void EnsureID () {
760                         if (NamingContainer == null)
761                                 return;
762                         EnsureIDInternal ();
763                         SetMask (ID_SET, true);
764                 }
765
766                 protected bool HasEvents () {
767                         return _events != null;
768                 }
769
770 #endif
771
772                 internal void ResetControlsCache ()
773                 {
774                         _controlsCache = null;
775
776                         if ((this.stateMask & IS_NAMING_CONTAINER) == 0 && Parent != null)
777                                 Parent.ResetControlsCache ();
778                 }
779
780                 internal Hashtable InitControlsCache ()
781                 {
782                         if (_controlsCache != null)
783                                 return _controlsCache;
784
785                         if ((this.stateMask & IS_NAMING_CONTAINER) != 0 || Parent == null)
786                                 _controlsCache = new Hashtable ();
787                         else
788                                 _controlsCache = Parent.InitControlsCache ();
789
790                         return _controlsCache;
791                 }
792
793                 void EnsureControlsCache ()
794                 {
795                         if (_controlsCache != null)
796                                 return;
797
798                         InitControlsCache ();
799                         FillControlCache (this);
800
801                 }
802
803                 void FillControlCache (Control control)
804                 {
805                         if (!HasControls ())
806                                 return;
807
808                         foreach (Control c in control._controls) {
809                                 try {
810                                         if (c._userId != null)
811                                                 _controlsCache.Add (c._userId, c);
812                                 }
813                                 catch (Exception ex) {
814                                         throw new HttpException ("Found more than one control with ID '" + c._userId + "'");
815                                 }
816
817                                 if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ())
818                                         FillControlCache (c);
819                         }
820                 }
821
822                 protected bool IsLiteralContent () {
823                         if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
824                                 return true;
825
826                         return false;
827                 }
828
829                 [WebSysDescription ("")]
830                 public virtual Control FindControl (string id) {
831                         return FindControl (id, 0);
832                 }
833
834                 Control LookForControlByName (string id) {
835 #if TARGET_J2EE
836                         if (this == Page && id != null && id == Page.PortletNamespace)
837                                 return this;
838 #endif
839                         EnsureControlsCache ();
840                         return (Control) _controlsCache [id];
841                 }
842
843                 protected virtual Control FindControl (string id, int pathOffset) {
844                         EnsureChildControls ();
845                         Control namingContainer = null;
846                         if ((stateMask & IS_NAMING_CONTAINER) == 0) {
847                                 namingContainer = NamingContainer;
848                                 if (namingContainer == null)
849                                         return null;
850
851                                 return namingContainer.FindControl (id, pathOffset);
852                         }
853
854                         if (!HasControls ())
855                                 return null;
856 #if NET_2_0
857                         int separatorIdx = id.IndexOf (IdSeparator, pathOffset);
858 #else
859                         int separatorIdx = id.IndexOf (':', pathOffset);
860 #endif
861                         if (separatorIdx == -1)
862                                 return LookForControlByName (id.Substring (pathOffset));
863
864                         string idfound = id.Substring (pathOffset, separatorIdx - pathOffset);
865                         namingContainer = LookForControlByName (idfound);
866                         if (namingContainer == null)
867                                 return null;
868
869                         return namingContainer.FindControl (id, separatorIdx + 1);
870                 }
871
872                 protected virtual void LoadViewState (object savedState) {
873                         if (savedState != null) {
874                                 ViewState.LoadViewState (savedState);
875                                 object o = ViewState ["Visible"];
876                                 if (o != null) {
877                                         SetMask (VISIBLE, (bool) o);
878                                         stateMask |= VISIBLE_CHANGED;
879                                 }
880                         }
881                 }
882
883                 // [MonoTODO("Secure?")]
884                 protected string MapPathSecure (string virtualPath) {
885                         string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
886                         return Context.Request.MapPath (combined);
887                 }
888
889                 protected virtual bool OnBubbleEvent (object source, EventArgs args) //DIT
890                 {
891 #if MONO_TRACE
892                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
893                         string type_name = null;
894                         if (trace != null) {
895                                 type_name = GetType ().Name;
896                                 trace.Write ("control", String.Format ("OnBubbleEvent {0} {1}", _userId, type_name));
897                         }
898 #endif
899                         return false;
900                 }
901
902                 protected virtual void OnDataBinding (EventArgs e) {
903                         if ((event_mask & databinding_mask) != 0) {
904                                 EventHandler eh = (EventHandler) (_events [DataBindingEvent]);
905                                 if (eh != null) {
906 #if MONO_TRACE
907                                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
908                                         string type_name = null;
909                                         if (trace != null) {
910                                                 type_name = GetType ().Name;
911                                                 trace.Write ("control", String.Format ("OnDataBinding {0} {1}", _userId, type_name));
912                                         }
913 #endif
914                                         eh (this, e);
915                                 }
916                         }
917                 }
918
919 #if NET_2_0
920                 protected internal
921 #else           
922                 protected
923 #endif
924  virtual void OnInit (EventArgs e) {
925                         if ((event_mask & init_mask) != 0) {
926                                 EventHandler eh = (EventHandler) (_events [InitEvent]);
927                                 if (eh != null) {
928 #if MONO_TRACE
929                                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
930                                         string type_name = null;
931                                         if (trace != null) {
932                                                 type_name = GetType ().Name;
933                                                 trace.Write ("control", String.Format ("OnInit {0} {1}", _userId, type_name));
934                                         }
935 #endif
936                                         eh (this, e);
937                                 }
938                         }
939                 }
940
941 #if NET_2_0
942                 protected internal
943 #else
944                 protected
945 #endif
946  virtual void OnLoad (EventArgs e) {
947                         if ((event_mask & load_mask) != 0) {
948                                 EventHandler eh = (EventHandler) (_events [LoadEvent]);
949                                 if (eh != null) {
950 #if MONO_TRACE
951                                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
952                                         string type_name = null;
953                                         if (trace != null) {
954                                                 type_name = GetType ().Name;
955                                                 trace.Write ("control", String.Format ("OnLoad {0} {1}", _userId, type_name));
956                                         }
957 #endif
958                                         eh (this, e);
959                                 }
960                         }
961                 }
962
963 #if NET_2_0
964                 protected internal
965 #else
966                 protected
967 #endif
968  virtual void OnPreRender (EventArgs e) {
969                         if ((event_mask & prerender_mask) != 0) {
970                                 EventHandler eh = (EventHandler) (_events [PreRenderEvent]);
971                                 if (eh != null) {
972 #if MONO_TRACE
973                                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
974                                         string type_name = null;
975                                         if (trace != null) {
976                                                 type_name = GetType ().Name;
977                                                 trace.Write ("control", String.Format ("OnPreRender {0} {1}", _userId, type_name));
978                                         }
979 #endif
980                                         eh (this, e);
981                                 }
982                         }
983                 }
984
985 #if NET_2_0
986                 protected internal
987 #else
988                 protected
989 #endif
990  virtual void OnUnload (EventArgs e) {
991                         if ((event_mask & unload_mask) != 0) {
992                                 EventHandler eh = (EventHandler) (_events [UnloadEvent]);
993                                 if (eh != null) {
994 #if MONO_TRACE
995                                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
996                                         string type_name = null;
997                                         if (trace != null) {
998                                                 type_name = GetType ().Name;
999                                                 trace.Write ("control", String.Format ("OnUnload {0} {1}", _userId, type_name));
1000                                         }
1001 #endif
1002                                         eh (this, e);
1003                                 }
1004                         }
1005                 }
1006
1007 #if NET_2_0
1008                 protected internal Stream OpenFile (string path) {
1009                         try {
1010                                 string filePath = Context.Server.MapPath (path);
1011                                 return File.OpenRead (filePath);
1012                         }
1013                         catch (UnauthorizedAccessException) {
1014                                 throw new HttpException ("Access to the specified file was denied.");
1015                         }
1016                 }
1017
1018                 internal string GetPhysicalFilePath (string virtualPath) {
1019                         Page page = Page;
1020
1021                         if (VirtualPathUtility.IsAbsolute (virtualPath))
1022                                 return page != null ? page.MapPath (virtualPath) : Context.Server.MapPath (virtualPath);
1023
1024                         // We need to determine whether one of our parents is a
1025                         // master page. If so, we need to map the path
1026                         // relatively to the master page and not our containing
1027                         // page/control. This is necessary for cases when a
1028                         // relative path is used in a control placed in a master
1029                         // page and the master page is referenced from a
1030                         // location other than its own. In such cases MS.NET looks
1031                         // for the file in the directory where the master page
1032                         // is.
1033                         //
1034                         // An example of where it is needed is at
1035                         //
1036                         // http://quickstarts.asp.net/QuickStartv20/aspnet/samples/masterpages/masterpages_cs/pages/default.aspx
1037                         //
1038                         MasterPage master = null;
1039                         Control ctrl = Parent;
1040
1041                         while (ctrl != null) {
1042                                 if (ctrl is MasterPage) {
1043                                         master = ctrl as MasterPage;
1044                                         break;
1045                                 }
1046                                 ctrl = ctrl.Parent;
1047                         }
1048
1049                         string path;
1050                         if (master != null)
1051                                 path = VirtualPathUtility.Combine (master.TemplateSourceDirectory, virtualPath);
1052                         else
1053                                 path = VirtualPathUtility.Combine (TemplateSourceDirectory, virtualPath);
1054                         return page != null ? page.MapPath (path) : Context.Server.MapPath (path);
1055                 }
1056 #endif
1057
1058                 protected void RaiseBubbleEvent (object source, EventArgs args) {
1059                         Control c = Parent;
1060                         while (c != null) {
1061 #if MONO_TRACE
1062                                 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1063                                 string type_name = null;
1064                                 if (trace != null) {
1065                                         type_name = GetType ().Name;
1066                                         trace.Write ("control", String.Format ("RaiseBubbleEvent {0} {1}", _userId, type_name));
1067                                 }
1068 #endif
1069                                 if (c.OnBubbleEvent (source, args)) {
1070 #if MONO_TRACE
1071                                         if (trace != null)
1072                                                 trace.Write ("control", String.Format ("End RaiseBubbleEvent (false) {0} {1}", _userId, type_name));
1073 #endif
1074                                         break;
1075                                 }
1076 #if MONO_TRACE
1077                                 if (trace != null)
1078                                         trace.Write ("control", String.Format ("End RaiseBubbleEvent (true) {0} {1}", _userId, type_name));
1079 #endif
1080                                 c = c.Parent;
1081                         }
1082                 }
1083
1084 #if NET_2_0
1085                 protected internal
1086 #else
1087                 protected
1088 #endif
1089  virtual void Render (HtmlTextWriter writer) //DIT
1090                                 {
1091                         RenderChildren (writer);
1092                 }
1093
1094 #if NET_2_0
1095                 protected internal
1096 #else
1097                 protected
1098 #endif
1099  virtual void RenderChildren (HtmlTextWriter writer) //DIT
1100                                 {
1101                         if (_renderMethodDelegate != null) {
1102                                 _renderMethodDelegate (writer, this);
1103                         }
1104                         else if (_controls != null) {
1105                                 int len = _controls.Count;
1106                                 Control c;
1107                                 for (int i = 0; i < len; i++) {
1108                                         c = _controls [i];
1109                                         if (c == null)
1110                                                 continue;
1111 #if NET_2_0
1112                                         if (c.Adapter != null)
1113                                                 c.RenderControl (writer, c.Adapter);
1114                                         else
1115 #endif
1116                                                 c.RenderControl (writer);
1117                                 }
1118                         }
1119                 }
1120
1121 #if NET_2_0
1122                 protected virtual ControlAdapter ResolveAdapter () {
1123                         throw new NotImplementedException ();
1124                 }
1125 #endif
1126
1127                 protected virtual object SaveViewState () {
1128                         if ((stateMask & VISIBLE_CHANGED) != 0) {
1129                                 ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
1130                         }
1131                         else if (_viewState == null) {
1132                                 return null;
1133                         }
1134
1135                         return _viewState.SaveViewState ();
1136                 }
1137
1138                 protected virtual void TrackViewState () {
1139                         if (_viewState != null)
1140                                 _viewState.TrackViewState ();
1141
1142                         stateMask |= TRACK_VIEWSTATE;
1143                 }
1144
1145                 public virtual void Dispose () {
1146                         if ((event_mask & disposed_mask) != 0) {
1147                                 EventHandler eh = (EventHandler) (_events [DisposedEvent]);
1148                                 if (eh != null)
1149                                         eh (this, EventArgs.Empty);
1150                         }
1151                 }
1152
1153                 [WebCategory ("FIXME")]
1154                 [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
1155                 public event EventHandler DataBinding {
1156                         add {
1157                                 event_mask |= databinding_mask;
1158                                 Events.AddHandler (DataBindingEvent, value);
1159                         }
1160                         remove { Events.RemoveHandler (DataBindingEvent, value); }
1161                 }
1162
1163                 [WebSysDescription ("Raised when the contol is disposed.")]
1164                 public event EventHandler Disposed {
1165                         add {
1166                                 event_mask |= disposed_mask;
1167                                 Events.AddHandler (DisposedEvent, value);
1168                         }
1169                         remove { Events.RemoveHandler (DisposedEvent, value); }
1170                 }
1171
1172                 [WebSysDescription ("Raised when the page containing the control is initialized.")]
1173                 public event EventHandler Init {
1174                         add {
1175                                 event_mask |= init_mask;
1176                                 Events.AddHandler (InitEvent, value);
1177                         }
1178                         remove { Events.RemoveHandler (InitEvent, value); }
1179                 }
1180
1181                 [WebSysDescription ("Raised after the page containing the control has been loaded.")]
1182                 public event EventHandler Load {
1183                         add {
1184                                 event_mask |= load_mask;
1185                                 Events.AddHandler (LoadEvent, value);
1186                         }
1187                         remove { Events.RemoveHandler (LoadEvent, value); }
1188                 }
1189
1190                 [WebSysDescription ("Raised before the page containing the control is rendered.")]
1191                 public event EventHandler PreRender {
1192                         add {
1193                                 event_mask |= prerender_mask;
1194                                 Events.AddHandler (PreRenderEvent, value);
1195                         }
1196                         remove { Events.RemoveHandler (PreRenderEvent, value); }
1197                 }
1198
1199                 [WebSysDescription ("Raised when the page containing the control is unloaded.")]
1200                 public event EventHandler Unload {
1201                         add {
1202                                 event_mask |= unload_mask;
1203                                 Events.AddHandler (UnloadEvent, value);
1204                         }
1205                         remove { Events.RemoveHandler (UnloadEvent, value); }
1206                 }
1207
1208                 public virtual void DataBind () //DIT
1209                 {
1210 #if NET_2_0
1211                         DataBind (true);
1212 #else
1213                         OnDataBinding (EventArgs.Empty);
1214                         DataBindChildren();
1215 #endif
1216                 }
1217
1218 #if NET_2_0
1219                 protected virtual
1220 #endif
1221
1222  void DataBindChildren () {
1223                         if (!HasControls ())
1224                                 return;
1225
1226                         int len = _controls.Count;
1227                         for (int i = 0; i < len; i++) {
1228                                 Control c = _controls [i];
1229                                 c.DataBind ();
1230                         }
1231                 }
1232
1233
1234                 public virtual bool HasControls () {
1235                         return (_controls != null && _controls.Count > 0);
1236                 }
1237
1238 #if NET_2_0
1239                 public virtual
1240 #else
1241                 public
1242 #endif
1243  void RenderControl (HtmlTextWriter writer) {
1244                         if ((stateMask & VISIBLE) != 0) {
1245                                 HttpContext ctx = Context;
1246                                 TraceContext trace = (ctx != null) ? ctx.Trace : null;
1247                                 int pos = 0;
1248                                 if ((trace != null) && trace.IsEnabled)
1249                                         pos = ctx.Response.GetOutputByteCount ();
1250
1251                                 Render (writer);
1252                                 if ((trace != null) && trace.IsEnabled) {
1253                                         int size = ctx.Response.GetOutputByteCount () - pos;
1254                                         trace.SaveSize (this, size >= 0 ? size : 0);
1255                                 }
1256                         }
1257                 }
1258
1259 #if NET_2_0
1260                 protected void RenderControl (HtmlTextWriter writer,
1261                                                   ControlAdapter adapter) {
1262                         if ((stateMask & VISIBLE) != 0) {
1263                                 adapter.BeginRender (writer);
1264                                 adapter.Render (writer);
1265                                 adapter.EndRender (writer);
1266                         }
1267                 }
1268 #endif
1269
1270                 public string ResolveUrl (string relativeUrl) {
1271                         if (relativeUrl == null)
1272                                 throw new ArgumentNullException ("relativeUrl");
1273
1274                         if (relativeUrl == "")
1275                                 return "";
1276
1277                         if (relativeUrl [0] == '#')
1278                                 return relativeUrl;
1279
1280                         string ts = TemplateSourceDirectory;
1281                         if (ts == null || ts.Length == 0 ||
1282                                 Context == null || Context.Response == null ||
1283                                 !UrlUtils.IsRelativeUrl (relativeUrl))
1284                                 return relativeUrl;
1285
1286                         HttpResponse resp = Context.Response;
1287                         return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
1288                 }
1289
1290
1291 #if NET_2_0
1292                 public
1293 #else
1294                 internal
1295 #endif
1296  string ResolveClientUrl (string relativeUrl) {
1297 #if TARGET_J2EE
1298                         // There are no relative paths when rendering a J2EE portlet
1299                         if (Page != null && Page.PortletNamespace != null)
1300                                 return ResolveUrl (relativeUrl);
1301 #endif
1302                         if (relativeUrl == null)
1303                                 throw new ArgumentNullException ("relativeUrl");
1304
1305                         if (relativeUrl.Length == 0)
1306                                 return String.Empty;
1307
1308                         if (VirtualPathUtility.IsAbsolute (relativeUrl) || relativeUrl.IndexOf (':') >= 0)
1309                                 return relativeUrl;
1310
1311                         HttpContext context = Context;
1312                         if (context != null && context.Request != null) {
1313                                 string templateSourceDirectory = TemplateSourceDirectory;
1314                                 if (templateSourceDirectory == null || templateSourceDirectory.Length == 0)
1315                                         return relativeUrl;
1316
1317                                 string basePath = context.Request.CurrentExecutionFilePath;
1318
1319                                 if (basePath.Length > 1 && basePath [basePath.Length - 1] != '/')
1320                                         basePath = VirtualPathUtility.GetDirectory (basePath);
1321
1322                                 if (VirtualPathUtility.IsAppRelative (relativeUrl))
1323                                         return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
1324
1325                                 string templatePath = VirtualPathUtility.AppendTrailingSlash (templateSourceDirectory);
1326
1327                                 if (basePath.Length == templatePath.Length && String.CompareOrdinal (basePath, templatePath) == 0)
1328                                         return relativeUrl;
1329
1330                                 relativeUrl = VirtualPathUtility.Combine (templatePath, relativeUrl);
1331                                 return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
1332                         }
1333                         return relativeUrl;
1334                 }
1335
1336                 internal bool HasRenderMethodDelegate () {
1337                         return _renderMethodDelegate != null;
1338                 }
1339
1340                 [EditorBrowsable (EditorBrowsableState.Advanced)]
1341                 public void SetRenderMethodDelegate (RenderMethod renderMethod) //DIT
1342                 {
1343                         _renderMethodDelegate = renderMethod;
1344                 }
1345
1346                 internal void LoadRecursive () {
1347 #if MONO_TRACE
1348                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1349                         string type_name = null;
1350                         if (trace != null) {
1351                                 type_name = GetType ().Name;
1352                                 trace.Write ("control", String.Format ("LoadRecursive {0} {1}", _userId, type_name));
1353                         }
1354 #endif
1355 #if NET_2_0
1356                         if (Adapter != null)
1357                                 Adapter.OnLoad (EventArgs.Empty);
1358                         else
1359 #endif
1360                                 OnLoad (EventArgs.Empty);
1361                         if (HasControls ()) {
1362                                 int len = _controls.Count;
1363                                 for (int i = 0; i < len; i++) {
1364                                         Control c = _controls [i];
1365                                         c.LoadRecursive ();
1366                                 }
1367                         }
1368
1369 #if MONO_TRACE
1370                         if (trace != null)
1371                                 trace.Write ("control", String.Format ("End LoadRecursive {0} {1}", _userId, type_name));
1372 #endif
1373                         stateMask |= LOADED;
1374                 }
1375
1376                 internal void UnloadRecursive (Boolean dispose) {
1377 #if MONO_TRACE
1378                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1379                         string type_name = null;
1380                         if (trace != null) {
1381                                 type_name = GetType ().Name;
1382                                 trace.Write ("control", String.Format ("UnloadRecursive {0} {1}", _userId, type_name));
1383                         }
1384 #endif
1385                         if (HasControls ()) {
1386                                 int len = _controls.Count;
1387                                 for (int i = 0; i < len; i++) {
1388                                         Control c = _controls [i];
1389                                         c.UnloadRecursive (dispose);
1390                                 }
1391                         }
1392
1393 #if MONO_TRACE
1394                         if (trace != null)
1395                                 trace.Write ("control", String.Format ("End UnloadRecursive {0} {1}", _userId, type_name));
1396 #endif
1397 #if NET_2_0
1398                         if (Adapter != null)
1399                                 Adapter.OnUnload (EventArgs.Empty);
1400                         else
1401 #endif
1402                                 OnUnload (EventArgs.Empty);
1403                         if (dispose)
1404                                 Dispose ();
1405                 }
1406
1407                 internal void PreRenderRecursiveInternal () {
1408                         if ((stateMask & VISIBLE) != 0) {
1409                                 EnsureChildControls ();
1410 #if MONO_TRACE
1411                                 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1412                                 string type_name = null;
1413                                 if (trace != null) {
1414                                         type_name = GetType ().Name;
1415                                         trace.Write ("control", String.Format ("PreRenderRecursive {0} {1}", _userId, type_name));
1416                                 }
1417 #endif
1418 #if NET_2_0
1419                                 if (Adapter != null)
1420                                         Adapter.OnPreRender (EventArgs.Empty);
1421                                 else
1422 #endif
1423                                         OnPreRender (EventArgs.Empty);
1424                                 if (!HasControls ())
1425                                         return;
1426
1427                                 int len = _controls.Count;
1428                                 for (int i = 0; i < len; i++) {
1429                                         Control c = _controls [i];
1430                                         c.PreRenderRecursiveInternal ();
1431                                 }
1432 #if MONO_TRACE
1433                                 if (trace != null)
1434                                         trace.Write ("control", String.Format ("End PreRenderRecursive {0} {1}", _userId, type_name));
1435 #endif
1436                         }
1437                         stateMask |= PRERENDERED;
1438                 }
1439
1440                 internal void InitRecursive (Control namingContainer) {
1441 #if MONO_TRACE
1442                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1443                         string type_name = null;
1444                         if (trace != null) {
1445                                 type_name = GetType ().Name;
1446                                 trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
1447                         }
1448 #endif
1449                         SetNamingContainer (namingContainer);
1450
1451                         if (HasControls ()) {
1452                                 if ((stateMask & IS_NAMING_CONTAINER) != 0)
1453                                         namingContainer = this;
1454
1455                                 int len = _controls.Count;
1456                                 for (int i = 0; i < len; i++) {
1457                                         Control c = _controls [i];
1458                                         c.InitRecursive (namingContainer);
1459                                 }
1460                         }
1461
1462                         stateMask |= INITING;
1463 #if NET_2_0
1464                         ApplyTheme ();
1465
1466                         if (Adapter != null)
1467                                 Adapter.OnInit (EventArgs.Empty);
1468                         else
1469 #endif
1470                                 OnInit (EventArgs.Empty);
1471 #if MONO_TRACE
1472                         if (trace != null)
1473                                 trace.Write ("control", String.Format ("End InitRecursive {0} {1}", _userId, type_name));
1474 #endif
1475                         TrackViewState ();
1476                         stateMask |= INITED;
1477                         stateMask &= ~INITING;
1478                 }
1479
1480                 internal object SaveViewStateRecursive () {
1481                         if (!EnableViewState)
1482                                 return null;
1483
1484 #if MONO_TRACE
1485                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1486                         string type_name = null;
1487                         if (trace != null) {
1488                                 type_name = GetType ().Name;
1489                                 trace.Write ("control", String.Format ("SaveViewStateRecursive {0} {1}", _userId, type_name));
1490                         }
1491 #endif
1492
1493                         ArrayList controlList = null;
1494                         ArrayList controlStates = null;
1495
1496                         int idx = -1;
1497                         if (HasControls ()) {
1498                                 int len = _controls.Count;
1499                                 for (int i = 0; i < len; i++) {
1500                                         Control ctrl = _controls [i];
1501                                         object ctrlState = ctrl.SaveViewStateRecursive ();
1502                                         idx++;
1503                                         if (ctrlState == null)
1504                                                 continue;
1505
1506                                         if (controlList == null) {
1507                                                 controlList = new ArrayList ();
1508                                                 controlStates = new ArrayList ();
1509                                         }
1510
1511                                         controlList.Add (idx);
1512                                         controlStates.Add (ctrlState);
1513                                 }
1514                         }
1515
1516                         object thisState = SaveViewState ();
1517                         if (thisState == null && controlList == null && controlStates == null) {
1518 #if MONO_TRACE
1519                                 if (trace != null) {
1520                                         trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved nothing", _userId, type_name));
1521                                         trace.SaveViewState (this, null);
1522                                 }
1523 #endif
1524                                 return null;
1525                         }
1526
1527 #if MONO_TRACE
1528                         if (trace != null) {
1529                                 trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved a Triplet", _userId, type_name));
1530                                 trace.SaveViewState (this, thisState);
1531                         }
1532 #endif
1533                         return new Triplet (thisState, controlList, controlStates);
1534                 }
1535
1536                 internal void LoadViewStateRecursive (object savedState) {
1537                         if (!EnableViewState || savedState == null)
1538                                 return;
1539
1540 #if MONO_TRACE
1541                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1542                         string type_name = null;
1543                         if (trace != null) {
1544                                 type_name = GetType ().Name;
1545                                 trace.Write ("control", String.Format ("LoadViewStateRecursive {0} {1}", _userId, type_name));
1546                         }
1547 #endif
1548                         Triplet savedInfo = (Triplet) savedState;
1549                         LoadViewState (savedInfo.First);
1550
1551                         ArrayList controlList = savedInfo.Second as ArrayList;
1552                         if (controlList == null)
1553                                 return;
1554                         ArrayList controlStates = savedInfo.Third as ArrayList;
1555                         int nControls = controlList.Count;
1556                         for (int i = 0; i < nControls; i++) {
1557                                 int k = (int) controlList [i];
1558                                 if (k < Controls.Count && controlStates != null) {
1559                                         Control c = Controls [k];
1560                                         c.LoadViewStateRecursive (controlStates [i]);
1561                                 }
1562                                 else {
1563                                         if (pendingVS == null)
1564                                                 pendingVS = new Hashtable ();
1565
1566                                         pendingVS [k] = controlStates [i];
1567                                 }
1568                         }
1569
1570 #if MONO_TRACE
1571                         if (trace != null)
1572                                 trace.Write ("control", String.Format ("End LoadViewStateRecursive {0} {1}", _userId, type_name));
1573 #endif
1574                         stateMask |= VIEWSTATE_LOADED;
1575                 }
1576
1577 #if NET_2_0
1578                 internal ControlSkin controlSkin;
1579
1580                 internal void ApplyTheme () {
1581 #if MONO_TRACE
1582                         TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1583                         string type_name = null;
1584                         if (trace != null) {
1585                                 type_name = GetType ().Name;
1586                                 trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
1587                         }
1588 #endif
1589                         if (Page.PageTheme != null && EnableTheming) {
1590                                 ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
1591                                 if (controlSkin != null)
1592                                         controlSkin.ApplySkin (this);
1593                         }
1594
1595 #if MONO_TRACE
1596                         if (trace != null)
1597                                 trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
1598 #endif
1599                 }
1600 #endif
1601
1602                 internal bool AutoID {
1603                         get { return (stateMask & AUTOID) != 0; }
1604                         set {
1605                                 if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
1606                                         return;
1607
1608                                 SetMask (AUTOID, value);
1609                         }
1610                 }
1611
1612                 protected internal virtual void RemovedControl (Control control) {
1613                         control.UnloadRecursive (false);
1614                         control._parent = null;
1615                         control._page = null;
1616                         control._namingContainer = null;
1617                         if ((control.stateMask & AUTOID_SET) != 0) {
1618                                 control._userId = null;
1619                                 control.SetMask (ID_SET, false);
1620                         }
1621                         control.NullifyUniqueID ();
1622                 }
1623
1624
1625 #if NET_2_0
1626
1627                 string skinId = string.Empty;
1628                 bool _enableTheming = true;
1629
1630                 [Browsable (false)]
1631                 [Themeable (false)]
1632                 [DefaultValue (true)]
1633                 public virtual bool EnableTheming {
1634                         get {
1635                                 if ((stateMask & ENABLE_THEMING) != 0)
1636                                         return _enableTheming;
1637
1638                                 if (_parent != null)
1639                                         return _parent.EnableTheming;
1640
1641                                 return true;
1642                         }
1643                         set {
1644                                 SetMask (ENABLE_THEMING, true);
1645                                 _enableTheming = value;
1646                         }
1647                 }
1648
1649                 [Browsable (false)]
1650                 [DefaultValue ("")]
1651                 [Filterable (false)]
1652                 public virtual string SkinID {
1653                         get { return skinId; }
1654                         set { skinId = value; }
1655                 }
1656
1657                 ControlBuilder IControlBuilderAccessor.ControlBuilder {
1658                         get { throw new NotImplementedException (); }
1659                 }
1660
1661                 IDictionary IControlDesignerAccessor.GetDesignModeState () {
1662                         throw new NotImplementedException ();
1663                 }
1664
1665                 void IControlDesignerAccessor.SetDesignModeState (IDictionary designData) {
1666                         SetDesignModeState (designData);
1667                 }
1668
1669                 void IControlDesignerAccessor.SetOwnerControl (Control control) {
1670                         throw new NotImplementedException ();
1671                 }
1672
1673                 IDictionary IControlDesignerAccessor.UserData {
1674                         get { throw new NotImplementedException (); }
1675                 }
1676
1677                 ExpressionBindingCollection expressionBindings;
1678
1679                 ExpressionBindingCollection IExpressionsAccessor.Expressions {
1680                         get {
1681                                 if (expressionBindings == null)
1682                                         expressionBindings = new ExpressionBindingCollection ();
1683                                 return expressionBindings;
1684                         }
1685                 }
1686
1687                 bool IExpressionsAccessor.HasExpressions {
1688                         get {
1689                                 return (expressionBindings != null && expressionBindings.Count > 0);
1690                         }
1691                 }
1692
1693                 public virtual void Focus () {
1694                         Page.SetFocus (this);
1695                 }
1696
1697                 protected internal virtual void LoadControlState (object state) {
1698                 }
1699
1700                 protected internal virtual object SaveControlState () {
1701                         return null;
1702                 }
1703
1704                 protected virtual void DataBind (bool raiseOnDataBinding) {
1705                         bool foundDataItem = false;
1706
1707                         if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
1708                                 object o = DataBinder.GetDataItem (this, out foundDataItem);
1709                                 if (foundDataItem)
1710                                         Page.PushDataItemContext (o);
1711                         }
1712
1713                         try {
1714
1715                                 if (raiseOnDataBinding)
1716                                         OnDataBinding (EventArgs.Empty);
1717                                 DataBindChildren ();
1718
1719                         }
1720                         finally {
1721                                 if (foundDataItem)
1722                                         Page.PopDataItemContext ();
1723                         }
1724                 }
1725
1726                 protected virtual IDictionary GetDesignModeState () {
1727                         throw new NotImplementedException ();
1728                 }
1729
1730                 protected virtual void SetDesignModeState (IDictionary data) {
1731                         throw new NotImplementedException ();
1732                 }
1733 #endif
1734                 void IParserAccessor.AddParsedSubObject (object obj) {
1735                         this.AddParsedSubObject (obj);
1736                 }
1737
1738                 DataBindingCollection IDataBindingsAccessor.DataBindings {
1739                         get {
1740                                 if (dataBindings == null) {
1741                                         dataBindings = new DataBindingCollection ();
1742                                 }
1743                                 return dataBindings;
1744                         }
1745                 }
1746
1747                 bool IDataBindingsAccessor.HasDataBindings {
1748                         get {
1749                                 if (dataBindings != null && dataBindings.Count > 0) {
1750                                         return true;
1751                                 }
1752                                 return false;
1753                         }
1754                 }
1755         }
1756 }