2 // System.Web.UI.UserControl.cs
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.ComponentModel;
33 using System.ComponentModel.Design;
34 using System.ComponentModel.Design.Serialization;
35 using System.Web.Caching;
36 using System.Web.SessionState;
38 namespace System.Web.UI
40 [ControlBuilder (typeof (UserControlControlBuilder))]
41 [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
43 [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
44 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
46 [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
47 [ParseChildren (true, "", ChildControlType = typeof (Control))]
49 [ParseChildren (true)]
51 public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
56 private bool initialized;
57 private AttributeCollection attributes;
58 private StateBag attrBag;
65 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
67 public HttpApplicationState Application
77 private void EnsureAttributes ()
79 if (attributes == null) {
80 attrBag = new StateBag (true);
81 if (IsTrackingViewState)
82 attrBag.TrackViewState ();
83 attributes = new AttributeCollection (attrBag);
87 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
89 public AttributeCollection Attributes
96 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
109 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
111 public ControlCachePolicy CachePolicy
114 throw new NotImplementedException ();
119 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121 public bool IsPostBack
131 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
133 public HttpRequest Request
143 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
145 public HttpResponse Response
155 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157 public HttpServerUtility Server
167 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
169 public HttpSessionState Session
179 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
181 public TraceContext Trace
191 [EditorBrowsable (EditorBrowsableState.Never)]
192 public void DesignerInitialize ()
194 InitRecursive (null);
197 [EditorBrowsable (EditorBrowsableState.Never)]
198 public void InitializeAsUserControl (Page page)
204 WireupAutomaticEvents ();
205 FrameworkInitialize ();
208 public string MapPath (string virtualPath)
210 return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
213 protected override void LoadViewState (object savedState)
215 if (savedState != null) {
216 Pair p = (Pair) savedState;
217 base.LoadViewState (p.First);
218 if (p.Second != null) {
220 attrBag.LoadViewState (p.Second);
231 override void OnInit (EventArgs e)
233 InitializeAsUserControl (Page);
238 protected override object SaveViewState ()
240 object baseState = base.SaveViewState();
241 object attrState = null;
242 if (attributes != null)
243 attrState = attrBag.SaveViewState ();
244 if (baseState == null && attrState == null)
246 return new Pair (baseState, attrState);
249 string IAttributeAccessor.GetAttribute (string name)
251 if (attributes == null)
253 return attributes [name];
256 void IAttributeAccessor.SetAttribute (string name, string value)
259 Attributes [name] = value;
262 string IUserControlDesignerAccessor.InnerText
265 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
266 if (innerText == null)
270 set { ViewState["!DesignTimeInnerText"] = value; }
273 string IUserControlDesignerAccessor.TagName
276 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
277 if (innerTag == null)
281 set { ViewState["!DesignTimeTagName"] = value; }