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;
46 using System.Globalization;
48 using System.Web.UI.Adapters;
52 namespace System.Web.UI
55 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
56 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
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")]
62 [DesignerSerializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VisualStudio_Web,
63 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
67 [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
68 "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
70 public partial class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
72 , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
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;
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;
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 TemplateControl _templateControl;
111 bool _isChildControlStateCleared;
113 string _templateSourceDirectory;
117 const int ENABLE_VIEWSTATE = 1;
118 const int VISIBLE = 1 << 1;
119 const int AUTOID = 1 << 2;
120 const int CREATING_CONTROLS = 1 << 3;
121 const int BINDING_CONTAINER = 1 << 4;
122 const int AUTO_EVENT_WIREUP = 1 << 5;
123 const int IS_NAMING_CONTAINER = 1 << 6;
124 const int VISIBLE_CHANGED = 1 << 7;
125 const int TRACK_VIEWSTATE = 1 << 8;
126 const int CHILD_CONTROLS_CREATED = 1 << 9;
127 const int ID_SET = 1 << 10;
128 const int INITED = 1 << 11;
129 const int INITING = 1 << 12;
130 const int VIEWSTATE_LOADED = 1 << 13;
131 const int LOADED = 1 << 14;
132 const int PRERENDERED = 1 << 15;
134 const int ENABLE_THEMING = 1 << 16;
140 defaultNameArray = new string [100];
141 for (int i = 0 ; i < 100 ; i++)
143 defaultNameArray [i] = String.Format("ctl{0:D2}", i);
145 defaultNameArray [i] = "_ctl" + i;
151 stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
152 if (this is INamingContainer)
153 stateMask |= IS_NAMING_CONTAINER;
157 [MonoTODO("Not implemented, always returns null")]
158 protected ControlAdapter Adapter
161 // for the time being, fool the
162 // Control machinery into thinking we
163 // don't have an Adapter. This will
164 // allow us to write all the rest of
165 // the Adapter handling code without
166 // having to worry about *having*
172 string _appRelativeTemplateSourceDirectory = null;
174 [EditorBrowsable (EditorBrowsableState.Advanced)]
176 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
177 public string AppRelativeTemplateSourceDirectory
180 if (_appRelativeTemplateSourceDirectory != null)
181 return _appRelativeTemplateSourceDirectory;
183 Control tc = TemplateControl;
185 return tc.AppRelativeTemplateSourceDirectory;
187 Control parent = Parent;
189 return parent.AppRelativeTemplateSourceDirectory;
191 HttpContext context = Context;
193 return VirtualPathUtility.ToAppRelative (
194 VirtualPathUtility.GetDirectory (context.Request.FilePath));
198 [EditorBrowsable (EditorBrowsableState.Never)]
199 set { _appRelativeTemplateSourceDirectory = value; }
204 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205 [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
206 public Control BindingContainer {
208 Control container = NamingContainer;
209 if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
210 container = container.BindingContainer;
215 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
217 [WebSysDescription ("An Identification of the control that is rendered.")]
218 public virtual string ClientID {
220 string client = UniqueID;
224 client = UniqueID2ClientID (client);
226 client = client.Replace (':', ClientIDSeparator);
235 internal string UniqueID2ClientID (string uniqueId)
237 return uniqueId.Replace (IdSeparator, ClientIDSeparator);
240 protected char ClientIDSeparator
242 char ClientIDSeparator
251 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
253 [WebSysDescription ("The child controls of this control.")]
254 public virtual ControlCollection Controls //DIT
258 if (_controls == null) _controls = CreateControlCollection();
264 [MonoTODO ("revisit once we have a real design strategy")]
265 protected internal bool DesignMode
267 get { return false; }
271 [DefaultValue (true), WebCategory ("Behavior")]
272 [WebSysDescription ("An Identification of the control that is rendered.")]
276 public virtual bool EnableViewState {
277 get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
278 set { SetMask (ENABLE_VIEWSTATE, value); }
281 [MergableProperty (false), ParenthesizePropertyName (true)]
282 [WebSysDescription ("The name of the control that is rendered.")]
284 [Filterable (false), Themeable (false)]
287 public virtual string ID {
289 return (((stateMask & ID_SET) != 0) ? _userId : null);
303 protected char IdSeparator
310 protected internal bool IsChildControlStateCleared {
311 get { return _isChildControlStateCleared; }
314 protected internal bool IsViewStateEnabled
318 for (Control control = this; control != null; control = control.Parent)
319 if (!control.EnableViewState)
326 protected bool LoadViewStateByID
334 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
336 [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
337 public virtual Control NamingContainer {
339 if (_namingContainer == null && _parent != null) {
340 if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
341 _namingContainer = _parent.NamingContainer;
343 _namingContainer = _parent;
346 return _namingContainer;
350 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
352 [WebSysDescription ("The webpage that this control resides on.")]
356 public virtual Page Page //DIT
360 if (_page == null && _parent != null) _page = _parent.Page;
369 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
371 [WebSysDescription ("The parent control of this control.")]
372 public virtual Control Parent //DIT
380 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
381 [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
382 [WebSysDescription ("The site this control is part of.")]
383 public ISite Site //DIT
397 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
398 public TemplateControl TemplateControl {
400 return TemplateControlInternal;
403 [EditorBrowsable (EditorBrowsableState.Never)]
404 set { _templateControl = value; }
407 internal virtual TemplateControl TemplateControlInternal {
409 if (_templateControl != null)
410 return _templateControl;
412 return _parent.TemplateControl;
419 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
421 [WebSysDescription ("A virtual directory containing the parent of the control.")]
422 public virtual string TemplateSourceDirectory {
425 if (_templateSourceDirectory == null) {
426 TemplateControl tc = TemplateControl;
429 HttpContext ctx = Context;
431 _templateSourceDirectory = VirtualPathUtility.GetDirectory (ctx.Request.CurrentExecutionFilePath);
432 } else if (tc != this)
433 _templateSourceDirectory = tc.TemplateSourceDirectory;
435 if (_templateSourceDirectory == null && this is TemplateControl) {
436 string path = ((TemplateControl)this).AppRelativeVirtualPath;
439 // Pretend our application virtual root is "/" even if it isn't - we just
440 // want to get an absolute url out of relative one, without the real
441 // application root prepended to it.
442 string ret = VirtualPathUtility.GetDirectory (VirtualPathUtility.ToAbsolute (path, "/"));
443 int len = ret.Length;
446 if (ret [--len] == '/')
447 _templateSourceDirectory = ret.Substring (0, len);
449 _templateSourceDirectory = String.Empty;
451 if (_templateSourceDirectory == null)
452 _templateSourceDirectory = String.Empty;
455 return _templateSourceDirectory;
457 if (_templateSourceDirectory == null) {
458 HttpContext ctx = HttpContext.Current;
459 HttpRequest req = ctx != null ? ctx.Request : null;
461 _templateSourceDirectory = (_parent == null)
462 ? req != null ? VirtualPathUtility.RemoveTrailingSlash (
463 VirtualPathUtility.GetDirectory (
464 HttpContext.Current.Request.CurrentExecutionFilePath)) : String.Empty
465 : _parent.TemplateSourceDirectory;
467 return _templateSourceDirectory;
473 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
475 [WebSysDescription ("The unique ID of the control.")]
476 public virtual string UniqueID {
478 if (uniqueID != null)
481 if (_namingContainer == null) {
482 if ((stateMask & IS_NAMING_CONTAINER) == 0)
483 _namingContainer = NamingContainer;
484 if (_namingContainer == null)
489 _userId = _namingContainer.GetDefaultName ();
491 string prefix = _namingContainer.UniqueID;
493 // For J2EE portlets we need to add the namespace to the ID.
494 if (_namingContainer == _page && _page.PortletNamespace != null)
495 prefix = _page.PortletNamespace;
498 if (_namingContainer == _page || prefix == null) {
504 uniqueID = prefix + IdSeparator + _userId;
506 uniqueID = prefix + ":" + _userId;
512 void SetMask (int m, bool val)
520 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
521 [WebSysDescription ("Visiblity state of the control.")]
522 public virtual bool Visible {
524 if ((stateMask & VISIBLE) == 0)
528 return _parent.Visible;
534 if ((value && (stateMask & VISIBLE) == 0) ||
535 (!value && (stateMask & VISIBLE) != 0)) {
536 if (IsTrackingViewState)
537 stateMask |= VISIBLE_CHANGED;
540 SetMask (VISIBLE, value);
544 protected bool ChildControlsCreated {
545 get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
547 if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
548 if (_controls != null)
552 SetMask (CHILD_CONTROLS_CREATED, value);
557 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
558 protected virtual HttpContext Context //DIT
563 if (_context != null)
566 return HttpContext.Current;
567 context = _parent.Context;
570 return HttpContext.Current;
574 protected EventHandlerList Events {
577 _events = new EventHandlerList ();
582 protected bool HasChildViewState {
584 return (pendingVS != null && pendingVS.Count > 0);
588 protected bool IsTrackingViewState {
589 get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
593 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
594 [WebSysDescription ("ViewState")]
595 protected virtual StateBag ViewState
599 if(_viewState == null)
600 _viewState = new StateBag (ViewStateIgnoresCase);
602 if (IsTrackingViewState)
603 _viewState.TrackViewState ();
610 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
611 protected virtual bool ViewStateIgnoresCase
618 internal bool AutoEventWireup {
619 get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
620 set { SetMask (AUTO_EVENT_WIREUP, value); }
623 internal void SetBindingContainer (bool isBC)
625 SetMask (BINDING_CONTAINER, isBC);
628 internal void ResetChildNames ()
633 string GetDefaultName ()
636 if (defaultNumberID > 99) {
638 defaultName = "ctl" + defaultNumberID++;
640 defaultName = "_ctl" + defaultNumberID++;
643 defaultName = defaultNameArray [defaultNumberID++];
648 void NullifyUniqueID ()
654 foreach (Control c in _controls)
655 c.NullifyUniqueID ();
658 protected internal virtual void AddedControl (Control control, int index)
660 /* Ensure the control don't have more than 1 parent */
661 if (control._parent != null)
662 control._parent.Controls.Remove (control);
664 control._parent = this;
665 control._page = _page;
666 Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
669 control._namingContainer = nc;
670 if (control.AutoID == true && control._userId == null)
671 control._userId = nc.GetDefaultName ();
674 if ((stateMask & (INITING | INITED)) != 0)
675 control.InitRecursive (nc);
677 if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
678 if (pendingVS != null) {
679 object vs = pendingVS [index];
681 pendingVS.Remove (index);
682 if (pendingVS.Count == 0)
685 control.LoadViewStateRecursive (vs);
690 if ((stateMask & LOADED) != 0)
691 control.LoadRecursive ();
693 if ((stateMask & PRERENDERED) != 0)
694 control.PreRenderRecursiveInternal ();
697 protected virtual void AddParsedSubObject(object obj) //DIT
699 Control c = obj as Control;
700 if (c != null) Controls.Add(c);
704 [EditorBrowsable (EditorBrowsableState.Advanced)]
705 public virtual void ApplyStyleSheetSkin (Page page)
707 if (!EnableTheming) /* this enough? */
710 /* apply the style sheet skin here */
711 if (page.StyleSheetPageTheme != null) {
712 ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType(), SkinID);
719 protected void BuildProfileTree(string parentId, bool calcViewState)
725 protected void ClearChildControlState ()
727 _isChildControlStateCleared = true;
730 protected void ClearChildState ()
732 ClearChildViewState ();
733 ClearChildControlState ();
737 protected void ClearChildViewState ()
747 virtual void CreateChildControls() {} //DIT
749 protected virtual ControlCollection CreateControlCollection() //DIT
751 return new ControlCollection(this);
754 protected virtual void EnsureChildControls ()
756 if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
757 stateMask |= CREATING_CONTROLS;
760 Adapter.CreateChildControls ();
763 CreateChildControls();
764 ChildControlsCreated = true;
765 stateMask &= ~CREATING_CONTROLS;
770 protected void EnsureID ()
774 if(String.IsNullOrEmpty(ID))
775 ID = NamingContainer.GetDefaultName ();
778 protected bool HasEvents ()
780 return _events != null;
786 protected bool IsLiteralContent()
788 if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
794 [WebSysDescription ("")]
795 public virtual Control FindControl (string id)
797 return FindControl (id, 0);
800 Control LookForControlByName (string id)
803 if (this == Page && id != null && id == Page.PortletNamespace)
809 Control result = null;
810 foreach (Control c in _controls) {
811 if (String.Compare (id, c._userId, true, CultureInfo.InvariantCulture) == 0) {
812 if (result != null && result != c) {
813 throw new HttpException ("1 Found more than one control with ID '" + id + "'");
820 if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ()) {
821 Control child = c.LookForControlByName (id);
823 if (result != null && result != child)
824 throw new HttpException ("2 Found more than one control with ID '" + id + "'");
834 protected virtual Control FindControl (string id, int pathOffset)
836 EnsureChildControls ();
837 Control namingContainer = null;
838 if ((stateMask & IS_NAMING_CONTAINER) == 0) {
839 namingContainer = NamingContainer;
840 if (namingContainer == null)
843 return namingContainer.FindControl (id, pathOffset);
849 int separatorIdx = id.IndexOf (IdSeparator, pathOffset);
851 int separatorIdx = id.IndexOf (':', pathOffset);
853 if (separatorIdx == -1)
854 return LookForControlByName (id.Substring (pathOffset));
856 string idfound = id.Substring (pathOffset, separatorIdx - pathOffset);
857 namingContainer = LookForControlByName (idfound);
858 if (namingContainer == null)
861 return namingContainer.FindControl (id, separatorIdx + 1);
864 protected virtual void LoadViewState(object savedState)
866 if (savedState != null) {
867 ViewState.LoadViewState (savedState);
868 object o = ViewState ["Visible"];
870 SetMask (VISIBLE, (bool) o);
871 stateMask |= VISIBLE_CHANGED;
876 // [MonoTODO("Secure?")]
877 protected string MapPathSecure(string virtualPath)
879 string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
880 return Context.Request.MapPath (combined);
883 protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
886 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
887 string type_name = null;
889 type_name = GetType ().Name;
890 trace.Write ("control", String.Format ("OnBubbleEvent {0} {1}", _userId, type_name));
896 protected virtual void OnDataBinding (EventArgs e)
898 if ((event_mask & databinding_mask) != 0) {
899 EventHandler eh = (EventHandler)(_events [DataBindingEvent]);
902 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
903 string type_name = null;
905 type_name = GetType ().Name;
906 trace.Write ("control", String.Format ("OnDataBinding {0} {1}", _userId, type_name));
919 virtual void OnInit (EventArgs e)
921 if ((event_mask & init_mask) != 0) {
922 EventHandler eh = (EventHandler)(_events [InitEvent]);
925 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
926 string type_name = null;
928 type_name = GetType ().Name;
929 trace.Write ("control", String.Format ("OnInit {0} {1}", _userId, type_name));
942 virtual void OnLoad (EventArgs e)
944 if ((event_mask & load_mask) != 0) {
945 EventHandler eh = (EventHandler)(_events [LoadEvent]);
948 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
949 string type_name = null;
951 type_name = GetType ().Name;
952 trace.Write ("control", String.Format ("OnLoad {0} {1}", _userId, type_name));
965 virtual void OnPreRender (EventArgs e)
967 if ((event_mask & prerender_mask) != 0) {
968 EventHandler eh = (EventHandler)(_events [PreRenderEvent]);
971 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
972 string type_name = null;
974 type_name = GetType ().Name;
975 trace.Write ("control", String.Format ("OnPreRender {0} {1}", _userId, type_name));
988 virtual void OnUnload(EventArgs e)
990 if ((event_mask & unload_mask) != 0) {
991 EventHandler eh = (EventHandler)(_events [UnloadEvent]);
994 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
995 string type_name = null;
997 type_name = GetType ().Name;
998 trace.Write ("control", String.Format ("OnUnload {0} {1}", _userId, type_name));
1007 protected internal Stream OpenFile (string path)
1010 string filePath = Context.Server.MapPath (path);
1011 return File.OpenRead (filePath);
1013 catch (UnauthorizedAccessException) {
1014 throw new HttpException ("Access to the specified file was denied.");
1018 internal string GetPhysicalFilePath (string virtualPath)
1022 if (VirtualPathUtility.IsAbsolute (virtualPath))
1023 return page != null ? page.MapPath (virtualPath) : Context.Server.MapPath (virtualPath);
1025 // We need to determine whether one of our parents is a
1026 // master page. If so, we need to map the path
1027 // relatively to the master page and not our containing
1028 // page/control. This is necessary for cases when a
1029 // relative path is used in a control placed in a master
1030 // page and the master page is referenced from a
1031 // location other than its own. In such cases MS.NET looks
1032 // for the file in the directory where the master page
1035 // An example of where it is needed is at
1037 // http://quickstarts.asp.net/QuickStartv20/aspnet/samples/masterpages/masterpages_cs/pages/default.aspx
1039 MasterPage master = null;
1040 Control ctrl = Parent;
1042 while (ctrl != null) {
1043 if (ctrl is MasterPage) {
1044 master = ctrl as MasterPage;
1052 path = VirtualPathUtility.Combine (master.TemplateSourceDirectory, virtualPath);
1054 path = VirtualPathUtility.Combine (TemplateSourceDirectory, virtualPath);
1055 return page != null ? page.MapPath (path) : Context.Server.MapPath (path);
1059 protected void RaiseBubbleEvent(object source, EventArgs args)
1064 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1065 string type_name = null;
1066 if (trace != null) {
1067 type_name = GetType ().Name;
1068 trace.Write ("control", String.Format ("RaiseBubbleEvent {0} {1}", _userId, type_name));
1071 if (c.OnBubbleEvent (source, args)) {
1074 trace.Write ("control", String.Format ("End RaiseBubbleEvent (false) {0} {1}", _userId, type_name));
1080 trace.Write ("control", String.Format ("End RaiseBubbleEvent (true) {0} {1}", _userId, type_name));
1091 virtual void Render(HtmlTextWriter writer) //DIT
1093 RenderChildren(writer);
1101 virtual void RenderChildren (HtmlTextWriter writer) //DIT
1103 if (_renderMethodDelegate != null) {
1104 _renderMethodDelegate (writer, this);
1105 } else if (_controls != null) {
1106 int len = _controls.Count;
1108 for (int i = 0; i < len; i++) {
1113 if (c.Adapter != null)
1114 c.RenderControl (writer, c.Adapter);
1117 c.RenderControl (writer);
1123 protected virtual ControlAdapter ResolveAdapter ()
1125 throw new NotImplementedException ();
1129 protected virtual object SaveViewState ()
1131 if ((stateMask & VISIBLE_CHANGED) != 0) {
1132 ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
1133 } else if (_viewState == null) {
1137 return _viewState.SaveViewState ();
1140 protected virtual void TrackViewState()
1142 if (_viewState != null)
1143 _viewState.TrackViewState ();
1145 stateMask |= TRACK_VIEWSTATE;
1148 public virtual void Dispose ()
1150 if ((event_mask & disposed_mask) != 0) {
1151 EventHandler eh = (EventHandler)(_events [DisposedEvent]);
1152 if (eh != null) eh (this, EventArgs.Empty);
1156 [WebCategory ("FIXME")]
1157 [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
1158 public event EventHandler DataBinding {
1160 event_mask |= databinding_mask;
1161 Events.AddHandler (DataBindingEvent, value);
1163 remove { Events.RemoveHandler (DataBindingEvent, value); }
1166 [WebSysDescription ("Raised when the contol is disposed.")]
1167 public event EventHandler Disposed {
1169 event_mask |= disposed_mask;
1170 Events.AddHandler (DisposedEvent, value);
1172 remove { Events.RemoveHandler (DisposedEvent, value); }
1175 [WebSysDescription ("Raised when the page containing the control is initialized.")]
1176 public event EventHandler Init {
1178 event_mask |= init_mask;
1179 Events.AddHandler (InitEvent, value);
1181 remove { Events.RemoveHandler (InitEvent, value); }
1184 [WebSysDescription ("Raised after the page containing the control has been loaded.")]
1185 public event EventHandler Load {
1187 event_mask |= load_mask;
1188 Events.AddHandler (LoadEvent, value);
1190 remove { Events.RemoveHandler (LoadEvent, value); }
1193 [WebSysDescription ("Raised before the page containing the control is rendered.")]
1194 public event EventHandler PreRender {
1196 event_mask |= prerender_mask;
1197 Events.AddHandler (PreRenderEvent, value);
1199 remove { Events.RemoveHandler (PreRenderEvent, value); }
1202 [WebSysDescription ("Raised when the page containing the control is unloaded.")]
1203 public event EventHandler Unload {
1205 event_mask |= unload_mask;
1206 Events.AddHandler (UnloadEvent, value);
1208 remove { Events.RemoveHandler (UnloadEvent, value); }
1211 public virtual void DataBind() //DIT
1216 OnDataBinding (EventArgs.Empty);
1225 void DataBindChildren ()
1227 if (!HasControls ())
1230 int len = _controls.Count;
1231 for (int i = 0; i < len; i++) {
1232 Control c = _controls [i];
1238 public virtual bool HasControls ()
1240 return (_controls != null && _controls.Count > 0);
1248 void RenderControl (HtmlTextWriter writer)
1250 if ((stateMask & VISIBLE) != 0) {
1251 HttpContext ctx = Context;
1252 TraceContext trace = (ctx != null) ? ctx.Trace : null;
1254 if ((trace != null) && trace.IsEnabled)
1255 pos = ctx.Response.GetOutputByteCount ();
1258 if ((trace != null) && trace.IsEnabled) {
1259 int size = ctx.Response.GetOutputByteCount () - pos;
1260 trace.SaveSize (this, size >= 0 ? size : 0);
1266 protected void RenderControl (HtmlTextWriter writer,
1267 ControlAdapter adapter)
1269 if ((stateMask & VISIBLE) != 0) {
1270 adapter.BeginRender (writer);
1271 adapter.Render (writer);
1272 adapter.EndRender (writer);
1277 public string ResolveUrl (string relativeUrl)
1279 if (relativeUrl == null)
1280 throw new ArgumentNullException ("relativeUrl");
1282 if (relativeUrl == "")
1285 if (relativeUrl [0] == '#')
1288 string ts = TemplateSourceDirectory;
1289 if (ts == null || ts.Length == 0 ||
1290 Context == null || Context.Response == null ||
1291 !UrlUtils.IsRelativeUrl (relativeUrl))
1294 HttpResponse resp = Context.Response;
1295 return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
1304 string ResolveClientUrl (string relativeUrl)
1307 // There are no relative paths when rendering a J2EE portlet
1308 if (Page != null && Page.PortletNamespace != null)
1309 return ResolveUrl (relativeUrl);
1311 if (relativeUrl == null)
1312 throw new ArgumentNullException ("relativeUrl");
1314 if (relativeUrl.Length == 0)
1315 return String.Empty;
1317 if (VirtualPathUtility.IsAbsolute (relativeUrl) || relativeUrl.IndexOf (':') >= 0)
1320 HttpContext context = Context;
1321 if (context != null && context.Request != null) {
1322 string templateSourceDirectory = TemplateSourceDirectory;
1323 if (templateSourceDirectory == null || templateSourceDirectory.Length == 0)
1326 string basePath = context.Request.CurrentExecutionFilePath;
1328 if (basePath.Length > 1 && basePath [basePath.Length - 1] != '/')
1329 basePath = VirtualPathUtility.GetDirectory (basePath);
1331 if(VirtualPathUtility.IsAppRelative(relativeUrl))
1332 return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
1334 string templatePath = VirtualPathUtility.AppendTrailingSlash (templateSourceDirectory);
1336 if (basePath.Length == templatePath.Length && String.CompareOrdinal (basePath, templatePath) == 0)
1339 relativeUrl = VirtualPathUtility.Combine (templatePath, relativeUrl);
1340 return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
1345 internal bool HasRenderMethodDelegate () {
1346 return _renderMethodDelegate != null;
1349 [EditorBrowsable (EditorBrowsableState.Advanced)]
1350 public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
1352 _renderMethodDelegate = renderMethod;
1355 internal void LoadRecursive()
1358 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1359 string type_name = null;
1360 if (trace != null) {
1361 type_name = GetType ().Name;
1362 trace.Write ("control", String.Format ("LoadRecursive {0} {1}", _userId, type_name));
1366 if (Adapter != null)
1367 Adapter.OnLoad (EventArgs.Empty);
1370 OnLoad (EventArgs.Empty);
1371 if (HasControls ()) {
1372 int len = _controls.Count;
1373 for (int i=0;i<len;i++)
1375 Control c = _controls[i];
1382 trace.Write ("control", String.Format ("End LoadRecursive {0} {1}", _userId, type_name));
1384 stateMask |= LOADED;
1387 internal void UnloadRecursive(Boolean dispose)
1390 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1391 string type_name = null;
1392 if (trace != null) {
1393 type_name = GetType ().Name;
1394 trace.Write ("control", String.Format ("UnloadRecursive {0} {1}", _userId, type_name));
1397 if (HasControls ()) {
1398 int len = _controls.Count;
1399 for (int i=0;i<len;i++)
1401 Control c = _controls[i];
1402 c.UnloadRecursive (dispose);
1408 trace.Write ("control", String.Format ("End UnloadRecursive {0} {1}", _userId, type_name));
1411 if (Adapter != null)
1412 Adapter.OnUnload (EventArgs.Empty);
1415 OnUnload (EventArgs.Empty);
1420 internal void PreRenderRecursiveInternal()
1422 if ((stateMask & VISIBLE) != 0) {
1423 EnsureChildControls ();
1425 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1426 string type_name = null;
1427 if (trace != null) {
1428 type_name = GetType ().Name;
1429 trace.Write ("control", String.Format ("PreRenderRecursive {0} {1}", _userId, type_name));
1433 if (Adapter != null)
1434 Adapter.OnPreRender (EventArgs.Empty);
1437 OnPreRender (EventArgs.Empty);
1438 if (!HasControls ())
1441 int len = _controls.Count;
1442 for (int i=0;i<len;i++)
1444 Control c = _controls[i];
1445 c.PreRenderRecursiveInternal ();
1449 trace.Write ("control", String.Format ("End PreRenderRecursive {0} {1}", _userId, type_name));
1452 stateMask |= PRERENDERED;
1455 internal void InitRecursive(Control namingContainer)
1458 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1459 string type_name = null;
1460 if (trace != null) {
1461 type_name = GetType ().Name;
1462 trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
1465 if (HasControls ()) {
1466 if ((stateMask & IS_NAMING_CONTAINER) != 0)
1467 namingContainer = this;
1469 if (namingContainer != null &&
1470 namingContainer._userId == null &&
1471 namingContainer.AutoID)
1472 namingContainer._userId = namingContainer.GetDefaultName () + "b";
1474 int len = _controls.Count;
1475 for (int i=0;i<len;i++)
1477 Control c = _controls[i];
1479 c._namingContainer = namingContainer;
1480 if (namingContainer != null && c._userId == null && c.AutoID)
1481 c._userId = namingContainer.GetDefaultName () + "c";
1482 c.InitRecursive (namingContainer);
1486 stateMask |= INITING;
1490 if (Adapter != null)
1491 Adapter.OnInit (EventArgs.Empty);
1494 OnInit (EventArgs.Empty);
1497 trace.Write ("control", String.Format ("End InitRecursive {0} {1}", _userId, type_name));
1500 stateMask |= INITED;
1501 stateMask &= ~INITING;
1504 internal object SaveViewStateRecursive ()
1506 if (!EnableViewState)
1510 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1511 string type_name = null;
1512 if (trace != null) {
1513 type_name = GetType ().Name;
1514 trace.Write ("control", String.Format ("SaveViewStateRecursive {0} {1}", _userId, type_name));
1518 ArrayList controlList = null;
1519 ArrayList controlStates = null;
1524 int len = _controls.Count;
1525 for (int i=0;i<len;i++)
1527 Control ctrl = _controls[i];
1528 object ctrlState = ctrl.SaveViewStateRecursive ();
1530 if (ctrlState == null)
1533 if (controlList == null)
1535 controlList = new ArrayList ();
1536 controlStates = new ArrayList ();
1539 controlList.Add (idx);
1540 controlStates.Add (ctrlState);
1544 object thisState = SaveViewState ();
1545 if (thisState == null && controlList == null && controlStates == null) {
1547 if (trace != null) {
1548 trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved nothing", _userId, type_name));
1549 trace.SaveViewState (this, null);
1556 if (trace != null) {
1557 trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved a Triplet", _userId, type_name));
1558 trace.SaveViewState (this, thisState);
1561 return new Triplet (thisState, controlList, controlStates);
1564 internal void LoadViewStateRecursive (object savedState)
1566 if (!EnableViewState || savedState == null)
1570 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1571 string type_name = null;
1572 if (trace != null) {
1573 type_name = GetType ().Name;
1574 trace.Write ("control", String.Format ("LoadViewStateRecursive {0} {1}", _userId, type_name));
1577 Triplet savedInfo = (Triplet) savedState;
1578 LoadViewState (savedInfo.First);
1580 ArrayList controlList = savedInfo.Second as ArrayList;
1581 if (controlList == null)
1583 ArrayList controlStates = savedInfo.Third as ArrayList;
1584 int nControls = controlList.Count;
1585 for (int i = 0; i < nControls; i++) {
1586 int k = (int) controlList [i];
1587 if (k < Controls.Count && controlStates != null) {
1588 Control c = Controls [k];
1589 c.LoadViewStateRecursive (controlStates [i]);
1591 if (pendingVS == null)
1592 pendingVS = new Hashtable ();
1594 pendingVS [k] = controlStates [i];
1600 trace.Write ("control", String.Format ("End LoadViewStateRecursive {0} {1}", _userId, type_name));
1602 stateMask |= VIEWSTATE_LOADED;
1606 internal ControlSkin controlSkin;
1608 internal void ApplyTheme ()
1611 TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
1612 string type_name = null;
1613 if (trace != null) {
1614 type_name = GetType ().Name;
1615 trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
1618 if (Page.PageTheme != null && EnableTheming) {
1619 ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
1620 if (controlSkin != null)
1621 controlSkin.ApplySkin (this);
1626 trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
1631 internal bool AutoID
1633 get { return (stateMask & AUTOID) != 0; }
1635 if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
1638 SetMask (AUTOID, value);
1642 protected internal virtual void RemovedControl (Control control)
1644 control.UnloadRecursive (false);
1645 control._parent = null;
1646 control._page = null;
1647 control._namingContainer = null;
1653 string skinId = string.Empty;
1654 bool _enableTheming = true;
1658 [DefaultValue (true)]
1659 public virtual bool EnableTheming
1663 if ((stateMask & ENABLE_THEMING) != 0)
1664 return _enableTheming;
1666 if (_parent != null)
1667 return _parent.EnableTheming;
1673 SetMask (ENABLE_THEMING, true);
1674 _enableTheming = value;
1680 [Filterable (false)]
1681 public virtual string SkinID
1683 get { return skinId; }
1684 set { skinId = value; }
1687 ControlBuilder IControlBuilderAccessor.ControlBuilder {
1688 get {throw new NotImplementedException (); }
1691 IDictionary IControlDesignerAccessor.GetDesignModeState ()
1693 throw new NotImplementedException ();
1696 void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
1698 SetDesignModeState (designData);
1701 void IControlDesignerAccessor.SetOwnerControl (Control control)
1703 throw new NotImplementedException ();
1706 IDictionary IControlDesignerAccessor.UserData {
1707 get { throw new NotImplementedException (); }
1710 ExpressionBindingCollection expressionBindings;
1712 ExpressionBindingCollection IExpressionsAccessor.Expressions {
1714 if (expressionBindings == null)
1715 expressionBindings = new ExpressionBindingCollection ();
1716 return expressionBindings;
1720 bool IExpressionsAccessor.HasExpressions {
1722 return (expressionBindings != null && expressionBindings.Count > 0);
1726 public virtual void Focus()
1728 Page.SetFocus (this);
1731 protected internal virtual void LoadControlState (object state)
1735 protected internal virtual object SaveControlState ()
1740 protected virtual void DataBind (bool raiseOnDataBinding)
1742 bool foundDataItem = false;
1744 if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
1745 object o = DataBinder.GetDataItem (this, out foundDataItem);
1747 Page.PushDataItemContext (o);
1752 if (raiseOnDataBinding)
1753 OnDataBinding (EventArgs.Empty);
1758 Page.PopDataItemContext ();
1762 protected virtual IDictionary GetDesignModeState ()
1764 throw new NotImplementedException ();
1767 protected virtual void SetDesignModeState (IDictionary data)
1769 throw new NotImplementedException ();
1772 void IParserAccessor.AddParsedSubObject (object obj) {
1773 this.AddParsedSubObject (obj);
1776 DataBindingCollection IDataBindingsAccessor.DataBindings {
1778 if (dataBindings == null) {
1779 dataBindings = new DataBindingCollection ();
1781 return dataBindings;
1785 bool IDataBindingsAccessor.HasDataBindings {
1787 if (dataBindings != null && dataBindings.Count > 0) {