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 [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
45 public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
47 private bool initialized;
48 private AttributeCollection attributes;
49 private StateBag attrBag;
56 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
58 public HttpApplicationState Application
68 private void EnsureAttributes ()
70 if (attributes == null) {
71 attrBag = new StateBag (true);
72 if (IsTrackingViewState)
73 attrBag.TrackViewState ();
74 attributes = new AttributeCollection (attrBag);
78 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
80 public AttributeCollection Attributes
87 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
99 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101 public bool IsPostBack
111 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
113 public HttpRequest Request
123 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
125 public HttpResponse Response
135 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137 public HttpServerUtility Server
147 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
149 public HttpSessionState Session
159 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
161 public TraceContext Trace
171 [EditorBrowsable (EditorBrowsableState.Never)]
172 public void DesignerInitialize ()
174 InitRecursive (null);
177 [EditorBrowsable (EditorBrowsableState.Never)]
178 public void InitializeAsUserControl (Page page)
184 WireupAutomaticEvents ();
185 FrameworkInitialize ();
188 public string MapPath (string virtualPath)
190 return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
193 protected override void LoadViewState (object savedState)
195 if (savedState != null) {
196 Pair p = (Pair) savedState;
197 base.LoadViewState (p.First);
198 if (p.Second != null) {
200 attrBag.LoadViewState (p.Second);
206 protected override void OnInit (EventArgs e)
208 InitializeAsUserControl (Page);
213 protected override object SaveViewState ()
215 object baseState = base.SaveViewState();
216 object attrState = null;
217 if (attributes != null)
218 attrState = attrBag.SaveViewState ();
219 if (baseState == null && attrState == null)
221 return new Pair (baseState, attrState);
224 string IAttributeAccessor.GetAttribute (string name)
226 if (attributes == null)
228 return attributes [name];
231 void IAttributeAccessor.SetAttribute (string name, string value)
234 Attributes [name] = value;
237 string IUserControlDesignerAccessor.InnerText
240 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
241 if (innerText == null)
245 set { ViewState["!DesignTimeInnerText"] = value; }
248 string IUserControlDesignerAccessor.TagName
251 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
252 if (innerTag == null)
256 set { ViewState["!DesignTimeTagName"] = value; }