2 // System.Web.UI.Control.cs
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)
11 // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
12 // (C) 2004 Novell, Inc. (http://www.novell.com)
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
36 // This will provide extra information when trace is enabled. Might be too verbose.
39 using System.Collections;
40 using System.ComponentModel;
41 using System.ComponentModel.Design;
42 using System.ComponentModel.Design.Serialization;
43 using System.Security.Permissions;
45 using System.Web.Util;
47 using System.Web.UI.Adapters;
51 namespace System.Web.UI
54 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
55 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
57 [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
58 [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
59 [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
61 [DesignerSerializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VisualStudio_Web,
62 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
66 [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
67 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
69 public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
71 , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
74 static readonly object DataBindingEvent = new object();
75 static readonly object DisposedEvent = new object();
76 static readonly object InitEvent = new object();
77 static readonly object LoadEvent = new object();
78 static readonly object PreRenderEvent = new object();
79 static readonly object UnloadEvent = new object();
80 static string[] defaultNameArray;
83 const int databinding_mask = 1;
84 const int disposed_mask = 1 << 1;
85 const int init_mask = 1 << 2;
86 const int load_mask = 1 << 3;
87 const int prerender_mask = 1 << 4;
88 const int unload_mask = 1 << 5;
93 TemplateControl _templateControl;
94 ControlCollection _controls;
95 Control _namingContainer;
101 EventHandlerList _events;
102 RenderMethod _renderMethodDelegate;
105 DataBindingCollection dataBindings;
106 Hashtable pendingVS; // may hold unused viewstate data from child controls
110 bool _isChildControlStateCleared;
114 const int ENABLE_VIEWSTATE = 1;
115 const int VISIBLE = 1 << 1;
116 const int AUTOID = 1 << 2;
117 const int CREATING_CONTROLS = 1 << 3;
118 const int BINDING_CONTAINER = 1 << 4;
119 const int AUTO_EVENT_WIREUP = 1 << 5;
120 const int IS_NAMING_CONTAINER = 1 << 6;
121 const int VISIBLE_CHANGED = 1 << 7;
122 const int TRACK_VIEWSTATE = 1 << 8;
123 const int CHILD_CONTROLS_CREATED = 1 << 9;
124 const int ID_SET = 1 << 10;
125 const int INITED = 1 << 11;
126 const int INITING = 1 << 12;
127 const int VIEWSTATE_LOADED = 1 << 13;
128 const int LOADED = 1 << 14;
129 const int PRERENDERED = 1 << 15;
131 const int ENABLE_THEMING = 1 << 16;
137 defaultNameArray = new string [100];
138 for (int i = 0 ; i < 100 ; i++)
139 defaultNameArray [i] = "_ctl" + i;
144 stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
145 if (this is INamingContainer)
146 stateMask |= IS_NAMING_CONTAINER;
150 [MonoTODO("Not implemented, always returns null")]
151 protected ControlAdapter Adapter
154 // for the time being, fool the
155 // Control machinery into thinking we
156 // don't have an Adapter. This will
157 // allow us to write all the rest of
158 // the Adapter handling code without
159 // having to worry about *having*
165 string _appRelativeTemplateSourceDirectory = "~/";
167 [EditorBrowsable (EditorBrowsableState.Advanced)]
169 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
170 public string AppRelativeTemplateSourceDirectory
172 get { return _appRelativeTemplateSourceDirectory; }
173 [EditorBrowsable (EditorBrowsableState.Never)]
174 set { _appRelativeTemplateSourceDirectory = value; }
179 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
180 [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
181 public Control BindingContainer {
183 Control container = NamingContainer;
184 if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
185 container = container.BindingContainer;
190 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
192 [WebSysDescription ("An Identification of the control that is rendered.")]
193 public virtual string ClientID {
195 string client = UniqueID;
198 client = client.Replace (':', ClientIDSeparator);
206 protected char ClientIDSeparator
208 char ClientIDSeparator
217 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
219 [WebSysDescription ("The child controls of this control.")]
220 public virtual ControlCollection Controls //DIT
224 if (_controls == null) _controls = CreateControlCollection();
230 [MonoTODO ("revisit once we have a real design strategy")]
231 protected internal bool DesignMode
233 get { return false; }
237 [DefaultValue (true), WebCategory ("Behavior")]
238 [WebSysDescription ("An Identification of the control that is rendered.")]
242 public virtual bool EnableViewState {
243 get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
244 set { SetMask (ENABLE_VIEWSTATE, value); }
247 [MergableProperty (false), ParenthesizePropertyName (true)]
248 [WebSysDescription ("The name of the control that is rendered.")]
250 [Filterable (false), Themeable (false)]
253 public virtual string ID {
255 return (((stateMask & ID_SET) != 0) ? _userId : null);
269 protected char IdSeparator
276 protected internal bool IsChildControlStateCleared {
277 get { return _isChildControlStateCleared; }
280 protected internal bool IsViewStateEnabled
286 for (Control control = this; control != null; control = control.Parent)
287 if (!control.EnableViewState)
294 protected bool LoadViewStateByID
302 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
304 [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
305 public virtual Control NamingContainer {
307 if (_namingContainer == null && _parent != null) {
308 if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
309 _namingContainer = _parent.NamingContainer;
311 _namingContainer = _parent;
314 return _namingContainer;
318 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
320 [WebSysDescription ("The webpage that this control resides on.")]
324 public virtual Page Page //DIT
328 if (_page == null && _parent != null) _page = _parent.Page;
337 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
339 [WebSysDescription ("The parent control of this control.")]
340 public virtual Control Parent //DIT
348 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
349 [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
350 [WebSysDescription ("The site this control is part of.")]
351 public ISite Site //DIT
365 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
366 public TemplateControl TemplateControl
369 return _templateControl;
371 [EditorBrowsable (EditorBrowsableState.Never)]
373 _templateControl = value;
378 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
380 [WebSysDescription ("A virtual directory containing the parent of the control.")]
381 public virtual string TemplateSourceDirectory {
382 get { return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory; }
385 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
387 [WebSysDescription ("The unique ID of the control.")]
388 public virtual string UniqueID {
390 if (uniqueID != null)
393 if (_namingContainer == null) {
394 if ((stateMask & IS_NAMING_CONTAINER) == 0)
395 _namingContainer = NamingContainer;
396 if (_namingContainer == null)
401 _userId = _namingContainer.GetDefaultName ();
403 string prefix = _namingContainer.UniqueID;
404 if (_namingContainer == _page || prefix == null) {
409 uniqueID = prefix + ":" + _userId;
414 void SetMask (int m, bool val)
422 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
423 [WebSysDescription ("Visiblity state of the control.")]
424 public virtual bool Visible {
426 if ((stateMask & VISIBLE) == 0)
430 return _parent.Visible;
436 if ((value && (stateMask & VISIBLE) == 0) ||
437 (!value && (stateMask & VISIBLE) != 0)) {
438 if (IsTrackingViewState)
439 stateMask |= VISIBLE_CHANGED;
442 SetMask (VISIBLE, value);
446 protected bool ChildControlsCreated {
447 get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
449 if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
450 if (_controls != null)
454 SetMask (CHILD_CONTROLS_CREATED, value);
459 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
460 protected virtual HttpContext Context //DIT
465 if (_context != null)
468 return HttpContext.Current;
469 context = _parent.Context;
472 return HttpContext.Current;
476 protected EventHandlerList Events {
479 _events = new EventHandlerList ();
484 protected bool HasChildViewState {
486 return (pendingVS != null && pendingVS.Count > 0);
490 protected bool IsTrackingViewState {
491 get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
495 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
496 [WebSysDescription ("ViewState")]
497 protected virtual StateBag ViewState
501 if(_viewState == null)
502 _viewState = new StateBag (ViewStateIgnoresCase);
504 if (IsTrackingViewState)
505 _viewState.TrackViewState ();
512 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
513 protected virtual bool ViewStateIgnoresCase
520 internal bool AutoEventWireup {
521 get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
522 set { SetMask (AUTO_EVENT_WIREUP, value); }
525 internal void SetBindingContainer (bool isBC)
527 SetMask (BINDING_CONTAINER, isBC);
530 internal void ResetChildNames ()
535 string GetDefaultName ()
538 if (defaultNumberID > 99) {
539 defaultName = "_ctl" + defaultNumberID++;
541 defaultName = defaultNameArray [defaultNumberID++];
546 void NullifyUniqueID ()
552 foreach (Control c in _controls)
553 c.NullifyUniqueID ();
556 protected internal virtual void AddedControl (Control control, int index)
558 /* Ensure the control don't have more than 1 parent */
559 if (control._parent != null)
560 control._parent.Controls.Remove (control);
562 control._parent = this;
563 control._page = _page;
564 Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
567 control._namingContainer = nc;
568 if (control.AutoID == true && control._userId == null)
569 control._userId = nc.GetDefaultName ();
572 if ((stateMask & (INITING | INITED)) != 0)
573 control.InitRecursive (nc);
575 if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
576 if (pendingVS != null) {
577 object vs = pendingVS [index];
579 pendingVS.Remove (index);
580 if (pendingVS.Count == 0)
583 control.LoadViewStateRecursive (vs);
588 if ((stateMask & LOADED) != 0)
589 control.LoadRecursive ();
591 if ((stateMask & PRERENDERED) != 0)
592 control.PreRenderRecursiveInternal ();
595 protected virtual void AddParsedSubObject(object obj) //DIT
597 Control c = obj as Control;
598 if (c != null) Controls.Add(c);
602 [EditorBrowsable (EditorBrowsableState.Advanced)]
603 public virtual void ApplyStyleSheetSkin (Page page)
605 if (!EnableTheming) /* this enough? */
608 /* apply the style sheet skin here */
609 if (page.StyleSheetPageTheme != null) {
610 ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType(), SkinID);
617 protected void BuildProfileTree(string parentId, bool calcViewState)
623 protected void ClearChildControlState ()
625 _isChildControlStateCleared = true;
628 protected void ClearChildState ()
630 ClearChildViewState ();
631 ClearChildControlState ();
635 protected void ClearChildViewState ()
645 virtual void CreateChildControls() {} //DIT
647 protected virtual ControlCollection CreateControlCollection() //DIT
649 return new ControlCollection(this);
652 protected virtual void EnsureChildControls ()
654 if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
655 stateMask |= CREATING_CONTROLS;
658 Adapter.CreateChildControls ();
661 CreateChildControls();
662 ChildControlsCreated = true;
663 stateMask &= ~CREATING_CONTROLS;
668 protected void EnsureID ()
672 ID = NamingContainer.GetDefaultName ();
675 protected bool HasEvents ()
677 throw new NotImplementedException ();
683 protected bool IsLiteralContent()
685 if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
691 [WebSysDescription ("")]
692 public virtual Control FindControl (string id)
694 return FindControl (id, 0);
697 Control LookForControlByName (string id)
702 Control result = null;
703 foreach (Control c in _controls) {
704 if (String.Compare (id, c._userId, true) == 0) {
705 if (result != null && result != c) {
706 throw new HttpException ("1 Found more than one control with ID '" + id + "'");
713 if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ()) {
714 Control child = c.LookForControlByName (id);
716 if (result != null && result != child)
717 throw new HttpException ("2 Found more than one control with ID '" + id + "'");
727 protected virtual Control FindControl (string id, int pathOffset)
729 EnsureChildControls ();
730 Control namingContainer = null;
731 if ((stateMask & IS_NAMING_CONTAINER) == 0) {
732 namingContainer = NamingContainer;
733 if (namingContainer == null)
736 return namingContainer.FindControl (id, pathOffset);
742 int colon = id.IndexOf (':', pathOffset);
744 return LookForControlByName (id.Substring (pathOffset));
746 string idfound = id.Substring (pathOffset, colon - pathOffset);
747 namingContainer = LookForControlByName (idfound);
748 if (namingContainer == null)
751 return namingContainer.FindControl (id, colon + 1);
754 protected virtual void LoadViewState(object savedState)
756 if (savedState != null) {
757 ViewState.LoadViewState (savedState);
758 object o = ViewState ["Visible"];
760 SetMask (VISIBLE, (bool) o);
761 stateMask |= VISIBLE_CHANGED;
766 // [MonoTODO("Secure?")]
767 protected string MapPathSecure(string virtualPath)
769 string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
770 return Context.Request.MapPath (combined);
773 protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
776 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
777 string type_name = null;
779 type_name = GetType ().Name;
780 trace.Write ("control", String.Format ("OnBubbleEvent {0} {1}", _userId, type_name));
786 protected virtual void OnDataBinding (EventArgs e)
788 if ((event_mask & databinding_mask) != 0) {
789 EventHandler eh = (EventHandler)(_events [DataBindingEvent]);
792 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
793 string type_name = null;
795 type_name = GetType ().Name;
796 trace.Write ("control", String.Format ("OnDataBinding {0} {1}", _userId, type_name));
809 virtual void OnInit (EventArgs e)
811 if ((event_mask & init_mask) != 0) {
812 EventHandler eh = (EventHandler)(_events [InitEvent]);
815 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
816 string type_name = null;
818 type_name = GetType ().Name;
819 trace.Write ("control", String.Format ("OnInit {0} {1}", _userId, type_name));
832 virtual void OnLoad (EventArgs e)
834 if ((event_mask & load_mask) != 0) {
835 EventHandler eh = (EventHandler)(_events [LoadEvent]);
838 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
839 string type_name = null;
841 type_name = GetType ().Name;
842 trace.Write ("control", String.Format ("OnLoad {0} {1}", _userId, type_name));
855 virtual void OnPreRender (EventArgs e)
857 if ((event_mask & prerender_mask) != 0) {
858 EventHandler eh = (EventHandler)(_events [PreRenderEvent]);
861 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
862 string type_name = null;
864 type_name = GetType ().Name;
865 trace.Write ("control", String.Format ("OnPreRender {0} {1}", _userId, type_name));
878 virtual void OnUnload(EventArgs e)
880 if ((event_mask & unload_mask) != 0) {
881 EventHandler eh = (EventHandler)(_events [UnloadEvent]);
884 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
885 string type_name = null;
887 type_name = GetType ().Name;
888 trace.Write ("control", String.Format ("OnUnload {0} {1}", _userId, type_name));
897 protected internal Stream OpenFile (string path)
900 string filePath = Context.Server.MapPath (path);
901 return File.OpenRead (filePath);
903 catch (UnauthorizedAccessException) {
904 throw new HttpException ("Access to the specified file was denied.");
909 protected void RaiseBubbleEvent(object source, EventArgs args)
914 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
915 string type_name = null;
917 type_name = GetType ().Name;
918 trace.Write ("control", String.Format ("RaiseBubbleEvent {0} {1}", _userId, type_name));
921 if (c.OnBubbleEvent (source, args)) {
924 trace.Write ("control", String.Format ("End RaiseBubbleEvent (false) {0} {1}", _userId, type_name));
930 trace.Write ("control", String.Format ("End RaiseBubbleEvent (true) {0} {1}", _userId, type_name));
941 virtual void Render(HtmlTextWriter writer) //DIT
943 RenderChildren(writer);
951 virtual void RenderChildren (HtmlTextWriter writer) //DIT
953 if (_renderMethodDelegate != null) {
954 _renderMethodDelegate (writer, this);
955 } else if (_controls != null) {
956 int len = _controls.Count;
957 for (int i = 0; i < len; i++) {
958 Control c = _controls [i];
960 if (c.Adapter != null)
961 c.RenderControl (writer, c.Adapter);
964 c.RenderControl (writer);
970 protected virtual ControlAdapter ResolveAdapter ()
972 throw new NotImplementedException ();
976 protected virtual object SaveViewState ()
978 if ((stateMask & VISIBLE_CHANGED) != 0) {
979 ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
980 } else if (_viewState == null) {
984 return _viewState.SaveViewState ();
987 protected virtual void TrackViewState()
989 if (_viewState != null)
990 _viewState.TrackViewState ();
992 stateMask |= TRACK_VIEWSTATE;
995 public virtual void Dispose ()
997 if ((event_mask & disposed_mask) != 0) {
998 EventHandler eh = (EventHandler)(_events [DisposedEvent]);
999 if (eh != null) eh (this, EventArgs.Empty);
1003 [WebCategory ("FIXME")]
1004 [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
1005 public event EventHandler DataBinding {
1007 event_mask |= databinding_mask;
1008 Events.AddHandler (DataBindingEvent, value);
1010 remove { Events.RemoveHandler (DataBindingEvent, value); }
1013 [WebSysDescription ("Raised when the contol is disposed.")]
1014 public event EventHandler Disposed {
1016 event_mask |= disposed_mask;
1017 Events.AddHandler (DisposedEvent, value);
1019 remove { Events.RemoveHandler (DisposedEvent, value); }
1022 [WebSysDescription ("Raised when the page containing the control is initialized.")]
1023 public event EventHandler Init {
1025 event_mask |= init_mask;
1026 Events.AddHandler (InitEvent, value);
1028 remove { Events.RemoveHandler (InitEvent, value); }
1031 [WebSysDescription ("Raised after the page containing the control has been loaded.")]
1032 public event EventHandler Load {
1034 event_mask |= load_mask;
1035 Events.AddHandler (LoadEvent, value);
1037 remove { Events.RemoveHandler (LoadEvent, value); }
1040 [WebSysDescription ("Raised before the page containing the control is rendered.")]
1041 public event EventHandler PreRender {
1043 event_mask |= prerender_mask;
1044 Events.AddHandler (PreRenderEvent, value);
1046 remove { Events.RemoveHandler (PreRenderEvent, value); }
1049 [WebSysDescription ("Raised when the page containing the control is unloaded.")]
1050 public event EventHandler Unload {
1052 event_mask |= unload_mask;
1053 Events.AddHandler (UnloadEvent, value);
1055 remove { Events.RemoveHandler (UnloadEvent, value); }
1058 public virtual void DataBind() //DIT
1063 OnDataBinding (EventArgs.Empty);
1072 void DataBindChildren ()
1074 if (!HasControls ())
1077 int len = _controls.Count;
1078 for (int i = 0; i < len; i++) {
1079 Control c = _controls [i];
1085 public virtual bool HasControls ()
1087 return (_controls != null && _controls.Count > 0);
1095 void RenderControl (HtmlTextWriter writer)
1097 if ((stateMask & VISIBLE) != 0) {
1098 HttpContext ctx = Context;
1099 TraceContext trace = (ctx != null) ? ctx.Trace : null;
1101 if ((trace != null) && trace.IsEnabled)
1102 pos = ctx.Response.GetOutputByteCount ();
1105 if ((trace != null) && trace.IsEnabled) {
1106 int size = ctx.Response.GetOutputByteCount () - pos;
1107 trace.SaveSize (this, size >= 0 ? size : 0);
1113 protected void RenderControl (HtmlTextWriter writer,
1114 ControlAdapter adapter)
1116 if ((stateMask & VISIBLE) != 0) {
1117 adapter.BeginRender (writer);
1118 adapter.Render (writer);
1119 adapter.EndRender (writer);
1124 public string ResolveUrl (string relativeUrl)
1126 if (relativeUrl == null)
1127 throw new ArgumentNullException ("relativeUrl");
1129 if (relativeUrl == "")
1132 if (relativeUrl [0] == '#')
1135 string ts = TemplateSourceDirectory;
1136 if (ts == "" || !UrlUtils.IsRelativeUrl (relativeUrl))
1139 HttpResponse resp = Context.Response;
1140 return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
1149 string ResolveClientUrl (string relativeUrl)
1151 string absoluteUrl = ResolveUrl (relativeUrl);
1152 if (Context != null && Context.Request != null) {
1153 string baseUrl = Context.Request.BaseVirtualDir;
1154 if (absoluteUrl.StartsWith (baseUrl + "/"))
1155 return absoluteUrl.Substring (baseUrl.Length + 1);
1160 internal bool HasRenderMethodDelegate () {
1161 return _renderMethodDelegate != null;
1164 [EditorBrowsable (EditorBrowsableState.Advanced)]
1165 public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
1167 _renderMethodDelegate = renderMethod;
1170 internal void LoadRecursive()
1173 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1174 string type_name = null;
1175 if (trace != null) {
1176 type_name = GetType ().Name;
1177 trace.Write ("control", String.Format ("LoadRecursive {0} {1}", _userId, type_name));
1181 if (Adapter != null)
1182 Adapter.OnLoad (EventArgs.Empty);
1185 OnLoad (EventArgs.Empty);
1186 if (HasControls ()) {
1187 int len = _controls.Count;
1188 for (int i=0;i<len;i++)
1190 Control c = _controls[i];
1197 trace.Write ("control", String.Format ("End LoadRecursive {0} {1}", _userId, type_name));
1199 stateMask |= LOADED;
1202 internal void UnloadRecursive(Boolean dispose)
1205 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1206 string type_name = null;
1207 if (trace != null) {
1208 type_name = GetType ().Name;
1209 trace.Write ("control", String.Format ("UnloadRecursive {0} {1}", _userId, type_name));
1212 if (HasControls ()) {
1213 int len = _controls.Count;
1214 for (int i=0;i<len;i++)
1216 Control c = _controls[i];
1217 c.UnloadRecursive (dispose);
1223 trace.Write ("control", String.Format ("End UnloadRecursive {0} {1}", _userId, type_name));
1226 if (Adapter != null)
1227 Adapter.OnUnload (EventArgs.Empty);
1230 OnUnload (EventArgs.Empty);
1235 internal void PreRenderRecursiveInternal()
1237 if ((stateMask & VISIBLE) != 0) {
1238 EnsureChildControls ();
1240 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1241 string type_name = null;
1242 if (trace != null) {
1243 type_name = GetType ().Name;
1244 trace.Write ("control", String.Format ("PreRenderRecursive {0} {1}", _userId, type_name));
1248 if (Adapter != null)
1249 Adapter.OnPreRender (EventArgs.Empty);
1252 OnPreRender (EventArgs.Empty);
1253 if (!HasControls ())
1256 int len = _controls.Count;
1257 for (int i=0;i<len;i++)
1259 Control c = _controls[i];
1260 c.PreRenderRecursiveInternal ();
1264 trace.Write ("control", String.Format ("End PreRenderRecursive {0} {1}", _userId, type_name));
1267 stateMask |= PRERENDERED;
1270 internal void InitRecursive(Control namingContainer)
1273 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1274 string type_name = null;
1275 if (trace != null) {
1276 type_name = GetType ().Name;
1277 trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
1280 if (HasControls ()) {
1281 if ((stateMask & IS_NAMING_CONTAINER) != 0)
1282 namingContainer = this;
1284 if (namingContainer != null &&
1285 namingContainer._userId == null &&
1286 namingContainer.AutoID)
1287 namingContainer._userId = namingContainer.GetDefaultName () + "b";
1289 int len = _controls.Count;
1290 for (int i=0;i<len;i++)
1292 Control c = _controls[i];
1294 c._namingContainer = namingContainer;
1295 if (namingContainer != null && c._userId == null && c.AutoID)
1296 c._userId = namingContainer.GetDefaultName () + "c";
1297 c.InitRecursive (namingContainer);
1301 stateMask |= INITING;
1305 if (Adapter != null)
1306 Adapter.OnInit (EventArgs.Empty);
1309 OnInit (EventArgs.Empty);
1312 trace.Write ("control", String.Format ("End InitRecursive {0} {1}", _userId, type_name));
1315 stateMask |= INITED;
1316 stateMask &= ~INITING;
1319 internal object SaveViewStateRecursive ()
1321 if (!EnableViewState)
1325 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1326 string type_name = null;
1327 if (trace != null) {
1328 type_name = GetType ().Name;
1329 trace.Write ("control", String.Format ("SaveViewStateRecursive {0} {1}", _userId, type_name));
1333 ArrayList controlList = null;
1334 ArrayList controlStates = null;
1339 int len = _controls.Count;
1340 for (int i=0;i<len;i++)
1342 Control ctrl = _controls[i];
1343 object ctrlState = ctrl.SaveViewStateRecursive ();
1345 if (ctrlState == null)
1348 if (controlList == null)
1350 controlList = new ArrayList ();
1351 controlStates = new ArrayList ();
1354 controlList.Add (idx);
1355 controlStates.Add (ctrlState);
1359 object thisState = SaveViewState ();
1360 if (thisState == null && controlList == null && controlStates == null) {
1362 if (trace != null) {
1363 trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved nothing", _userId, type_name));
1364 trace.SaveViewState (this, null);
1371 if (trace != null) {
1372 trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved a Triplet", _userId, type_name));
1373 trace.SaveViewState (this, thisState);
1376 return new Triplet (thisState, controlList, controlStates);
1379 internal void LoadViewStateRecursive (object savedState)
1381 if (!EnableViewState || savedState == null)
1385 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1386 string type_name = null;
1387 if (trace != null) {
1388 type_name = GetType ().Name;
1389 trace.Write ("control", String.Format ("LoadViewStateRecursive {0} {1}", _userId, type_name));
1392 Triplet savedInfo = (Triplet) savedState;
1393 LoadViewState (savedInfo.First);
1395 ArrayList controlList = savedInfo.Second as ArrayList;
1396 if (controlList == null)
1398 ArrayList controlStates = savedInfo.Third as ArrayList;
1399 int nControls = controlList.Count;
1400 for (int i = 0; i < nControls; i++) {
1401 int k = (int) controlList [i];
1402 if (k < Controls.Count && controlStates != null) {
1403 Control c = Controls [k];
1404 c.LoadViewStateRecursive (controlStates [i]);
1406 if (pendingVS == null)
1407 pendingVS = new Hashtable ();
1409 pendingVS [k] = controlStates [i];
1415 trace.Write ("control", String.Format ("End LoadViewStateRecursive {0} {1}", _userId, type_name));
1417 stateMask |= VIEWSTATE_LOADED;
1421 internal ControlSkin controlSkin;
1423 internal void ApplyTheme ()
1426 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1427 string type_name = null;
1428 if (trace != null) {
1429 type_name = GetType ().Name;
1430 trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
1433 if (Page.PageTheme != null && EnableTheming) {
1434 ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
1435 if (controlSkin != null)
1436 controlSkin.ApplySkin (this);
1441 trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
1446 internal bool AutoID
1448 get { return (stateMask & AUTOID) != 0; }
1450 if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
1453 SetMask (AUTOID, value);
1457 protected internal virtual void RemovedControl (Control control)
1459 control.UnloadRecursive (false);
1460 control._parent = null;
1461 control._page = null;
1462 control._namingContainer = null;
1468 string skinId = string.Empty;
1469 bool _enableTheming = true;
1473 [DefaultValue (true)]
1474 public virtual bool EnableTheming
1478 if ((stateMask & ENABLE_THEMING) != 0)
1479 return _enableTheming;
1481 if (_parent != null)
1482 return _parent.EnableTheming;
1488 SetMask (ENABLE_THEMING, true);
1489 _enableTheming = value;
1495 [Filterable (false)]
1496 public virtual string SkinID
1498 get { return skinId; }
1499 set { skinId = value; }
1502 ControlBuilder IControlBuilderAccessor.ControlBuilder {
1503 get {throw new NotImplementedException (); }
1506 IDictionary IControlDesignerAccessor.GetDesignModeState ()
1508 throw new NotImplementedException ();
1511 void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
1513 SetDesignModeState (designData);
1516 void IControlDesignerAccessor.SetOwnerControl (Control control)
1518 throw new NotImplementedException ();
1521 IDictionary IControlDesignerAccessor.UserData {
1522 get { throw new NotImplementedException (); }
1525 ExpressionBindingCollection expressionBindings;
1527 ExpressionBindingCollection IExpressionsAccessor.Expressions {
1529 if (expressionBindings == null)
1530 expressionBindings = new ExpressionBindingCollection ();
1531 return expressionBindings;
1535 bool IExpressionsAccessor.HasExpressions {
1537 return (expressionBindings != null && expressionBindings.Count > 0);
1541 public virtual void Focus()
1543 Page.SetFocus (this);
1546 protected internal virtual void LoadControlState (object state)
1550 protected internal virtual object SaveControlState ()
1555 protected virtual void DataBind (bool raiseOnDataBinding)
1557 bool foundDataItem = false;
1559 if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
1560 object o = DataBinder.GetDataItem (this, out foundDataItem);
1562 Page.PushDataItemContext (o);
1567 if (raiseOnDataBinding)
1568 OnDataBinding (EventArgs.Empty);
1573 Page.PopDataItemContext ();
1577 protected virtual IDictionary GetDesignModeState ()
1579 throw new NotImplementedException ();
1582 protected virtual void SetDesignModeState (IDictionary data)
1584 throw new NotImplementedException ();
1587 void IParserAccessor.AddParsedSubObject (object obj) {
1588 this.AddParsedSubObject (obj);
1591 DataBindingCollection IDataBindingsAccessor.DataBindings {
1593 if (dataBindings == null) {
1594 dataBindings = new DataBindingCollection ();
1596 return dataBindings;
1600 bool IDataBindingsAccessor.HasDataBindings {
1602 if (dataBindings != null && dataBindings.Count > 0) {