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")]
42 [ToolboxItem (false), ParseChildren (true)]
43 [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
44 [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
45 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
46 public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
48 private bool initialized;
49 private AttributeCollection attributes;
50 private StateBag attrBag;
57 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
59 public HttpApplicationState Application
69 private void EnsureAttributes ()
71 if (attributes == null) {
72 attrBag = new StateBag (true);
73 if (IsTrackingViewState)
74 attrBag.TrackViewState ();
75 attributes = new AttributeCollection (attrBag);
79 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
81 public AttributeCollection Attributes
88 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
100 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
102 public bool IsPostBack
112 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114 public HttpRequest Request
124 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
126 public HttpResponse Response
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
138 public HttpServerUtility Server
148 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
150 public HttpSessionState Session
160 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
162 public TraceContext Trace
172 [EditorBrowsable (EditorBrowsableState.Never)]
173 public void DesignerInitialize ()
175 InitRecursive (null);
178 [EditorBrowsable (EditorBrowsableState.Never)]
179 public void InitializeAsUserControl (Page page)
185 WireupAutomaticEvents ();
186 FrameworkInitialize ();
189 public string MapPath (string virtualPath)
191 return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
194 protected override void LoadViewState (object savedState)
196 if (savedState != null) {
197 Pair p = (Pair) savedState;
198 base.LoadViewState (p.First);
199 if (p.Second != null) {
201 attrBag.LoadViewState (p.Second);
207 protected override void OnInit (EventArgs e)
209 InitializeAsUserControl (Page);
214 protected override object SaveViewState ()
216 object baseState = base.SaveViewState();
217 object attrState = null;
218 if (attributes != null)
219 attrState = attrBag.SaveViewState ();
220 if (baseState == null && attrState == null)
222 return new Pair (baseState, attrState);
225 string IAttributeAccessor.GetAttribute (string name)
227 if (attributes == null)
229 return attributes [name];
232 void IAttributeAccessor.SetAttribute (string name, string value)
235 Attributes [name] = value;
238 string IUserControlDesignerAccessor.InnerText
241 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
242 if (innerText == null)
246 set { ViewState["!DesignTimeInnerText"] = value; }
249 string IUserControlDesignerAccessor.TagName
252 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
253 if (innerTag == null)
257 set { ViewState["!DesignTimeTagName"] = value; }