2005-06-08 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / UserControl.cs
1 //
2 // System.Web.UI.UserControl.cs
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 using System;
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;
37
38 namespace System.Web.UI
39 {
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
46         {
47                 private bool initialized;
48                 private AttributeCollection attributes;
49                 private StateBag attrBag;
50
51                 public UserControl ()
52                 {
53                         //??
54                 }
55
56                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
57                 [Browsable (false)]
58                 public HttpApplicationState Application
59                 {
60                         get {
61                                 Page p = Page;
62                                 if (p == null)
63                                         return null;
64                                 return p.Application;
65                         }
66                 }
67
68                 private void EnsureAttributes ()
69                 {
70                         if (attributes == null) {
71                                 attrBag = new StateBag (true);
72                                 if (IsTrackingViewState)
73                                         attrBag.TrackViewState ();
74                                 attributes = new AttributeCollection (attrBag);
75                         }
76                 }
77
78                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
79                 [Browsable (false)]
80                 public AttributeCollection Attributes
81                 {
82                         get {
83                                 return attributes;
84                         }
85                 }
86
87                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
88                 [Browsable (false)]
89                 public Cache Cache
90                 {
91                         get {
92                                 Page p = Page;
93                                 if (p == null)
94                                         return null;
95                                 return p.Cache;
96                         }
97                 }
98
99                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
100                 [Browsable (false)]
101                 public bool IsPostBack
102                 {
103                         get {
104                                 Page p = Page;
105                                 if (p == null)
106                                         return false;
107                                 return p.IsPostBack;
108                         }
109                 }
110
111                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
112                 [Browsable (false)]
113                 public HttpRequest Request
114                 {
115                         get {
116                                 Page p = Page;
117                                 if (p == null)
118                                         return null;
119                                 return p.Request;
120                         }
121                 }
122
123                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
124                 [Browsable (false)]
125                 public HttpResponse Response
126                 {
127                         get {
128                                 Page p = Page;
129                                 if (p == null)
130                                         return null;
131                                 return p.Response;
132                         }
133                 }
134
135                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
136                 [Browsable (false)]
137                 public HttpServerUtility Server
138                 {
139                         get {
140                                 Page p = Page;
141                                 if (p == null)
142                                         return null;
143                                 return p.Server;
144                         }
145                 }
146
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 [Browsable (false)]
149                 public HttpSessionState Session
150                 {
151                         get {
152                                 Page p = Page;
153                                 if (p == null)
154                                         return null;
155                                 return p.Session;
156                         }
157                 }
158
159                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
160                 [Browsable (false)]
161                 public TraceContext Trace
162                 {
163                         get {
164                                 Page p = Page;
165                                 if (p == null)
166                                         return null;
167                                 return p.Trace;
168                         }
169                 }
170
171                 [EditorBrowsable (EditorBrowsableState.Never)]
172                 public void DesignerInitialize ()
173                 {
174                         InitRecursive (null);
175                 }
176
177                 [EditorBrowsable (EditorBrowsableState.Never)]
178                 public void InitializeAsUserControl (Page page)
179                 {
180                         if (initialized)
181                                 return;
182                         initialized = true;
183                         this.Page = page;
184                         WireupAutomaticEvents ();
185                         FrameworkInitialize ();
186                 }
187
188                 public string MapPath (string virtualPath)
189                 {
190                         return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
191                 }
192
193                 protected override void LoadViewState (object savedState)
194                 {
195                         if (savedState != null) {
196                                 Pair p = (Pair) savedState;
197                                 base.LoadViewState (p.First);
198                                 if (p.Second != null) {
199                                         EnsureAttributes ();
200                                         attrBag.LoadViewState (p.Second);
201                                 }
202                         }
203
204                 }
205
206                 protected override void OnInit (EventArgs e)
207                 {
208                         InitializeAsUserControl (Page);
209
210                         base.OnInit(e);
211                 }
212
213                 protected override object SaveViewState ()
214                 {
215                         object baseState = base.SaveViewState();
216                         object attrState = null;
217                         if (attributes != null)
218                                 attrState = attrBag.SaveViewState ();
219                         if (baseState == null && attrState == null)
220                                 return null;
221                         return new Pair (baseState, attrState);
222                 }
223
224                 string IAttributeAccessor.GetAttribute (string name)
225                 {
226                         if (attributes == null)
227                                 return null;
228                         return attributes [name];
229                 }
230                 
231                 void IAttributeAccessor.SetAttribute (string name, string value)
232                 {
233                         EnsureAttributes ();
234                         Attributes [name] = value;
235                 }
236
237                 string IUserControlDesignerAccessor.InnerText
238                 {
239                         get {
240                                 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
241                                 if (innerText == null)
242                                         return string.Empty; 
243                                 return innerText;
244                         }
245                         set { ViewState["!DesignTimeInnerText"] = value; }
246                 }
247
248                 string IUserControlDesignerAccessor.TagName
249                 {
250                         get {
251                                 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
252                                 if (innerTag == null)
253                                         return string.Empty; 
254                                 return innerTag;
255                         }
256                         set { ViewState["!DesignTimeTagName"] = value; }
257                 }
258         }
259 }
260