2009-01-30 Miguel de Icaza <miguel@novell.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 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.ComponentModel.Design.Serialization;
34 using System.Security.Permissions;
35 using System.Web.Caching;
36 using System.Web.SessionState;
37
38 namespace System.Web.UI {
39
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         // attributes
44         [ControlBuilder (typeof (UserControlControlBuilder))]
45         [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
46         [ToolboxItem (false)]
47         [Designer ("System.Web.UI.Design.UserControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
48         [ParseChildren (true)]
49 #if NET_2_0
50         [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
51 #else
52         [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
53 #endif
54         public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
55 #if NET_2_0
56                 , INamingContainer, IFilterResolutionService, INonBindingContainer
57 #endif
58         {
59 #if NET_2_0
60                 ControlCachePolicy cachePolicy;
61 #endif
62                 bool initialized;
63                 AttributeCollection attributes;
64                 StateBag attrBag;
65
66                 public UserControl ()
67                 {
68                 }
69
70                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
71                 [Browsable (false)]
72                 public HttpApplicationState Application
73                 {
74                         get {
75                                 Page p = Page;
76                                 if (p == null)
77                                         return null;
78                                 return p.Application;
79                         }
80                 }
81
82                 void EnsureAttributes ()
83                 {
84                         if (attributes == null) {
85                                 attrBag = new StateBag (true);
86                                 if (IsTrackingViewState)
87                                         attrBag.TrackViewState ();
88                                 attributes = new AttributeCollection (attrBag);
89                         }
90                 }
91
92                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93                 [Browsable (false)]
94                 public AttributeCollection Attributes
95                 {
96                         get {
97                                 EnsureAttributes ();
98                                 return attributes;
99                         }
100                 }
101
102                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
103                 [Browsable (false)]
104                 public Cache Cache
105                 {
106                         get {
107                                 Page p = Page;
108                                 if (p == null)
109                                         return null;
110                                 return p.Cache;
111                         }
112                 }
113
114 #if NET_2_0
115                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116                 [Browsable (false)]
117                 public ControlCachePolicy CachePolicy 
118                 {
119                         get {
120                                 BasePartialCachingControl bpcc = Parent as BasePartialCachingControl;
121
122                                 if (bpcc != null)
123                                         return bpcc.CachePolicy;
124                                 
125                                 if (cachePolicy == null)
126                                         cachePolicy = new ControlCachePolicy ();
127                                 return cachePolicy;
128                         }
129                 }
130 #endif          
131
132                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
133                 [Browsable (false)]
134                 public bool IsPostBack
135                 {
136                         get {
137                                 Page p = Page;
138                                 if (p == null)
139                                         return false;
140                                 return p.IsPostBack;
141                         }
142                 }
143
144                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
145                 [Browsable (false)]
146                 public HttpRequest Request
147                 {
148                         get {
149                                 Page p = Page;
150                                 if (p == null)
151                                         return null;
152                                 return p.Request;
153                         }
154                 }
155
156                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157                 [Browsable (false)]
158                 public HttpResponse Response
159                 {
160                         get {
161                                 Page p = Page;
162                                 if (p == null)
163                                         return null;
164                                 return p.Response;
165                         }
166                 }
167
168                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
169                 [Browsable (false)]
170                 public HttpServerUtility Server
171                 {
172                         get {
173                                 Page p = Page;
174                                 if (p == null)
175                                         return null;
176                                 return p.Server;
177                         }
178                 }
179
180                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
181                 [Browsable (false)]
182                 public HttpSessionState Session
183                 {
184                         get {
185                                 Page p = Page;
186                                 if (p == null)
187                                         return null;
188                                 return p.Session;
189                         }
190                 }
191
192                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
193                 [Browsable (false)]
194                 public TraceContext Trace
195                 {
196                         get {
197                                 Page p = Page;
198                                 if (p == null)
199                                         return null;
200                                 return p.Trace;
201                         }
202                 }
203
204                 [EditorBrowsable (EditorBrowsableState.Never)]
205                 public void DesignerInitialize ()
206                 {
207                         InitRecursive (null);
208                 }
209
210                 [EditorBrowsable (EditorBrowsableState.Never)]
211                 public void InitializeAsUserControl (Page page)
212                 {
213                         if (initialized)
214                                 return;
215                         this.Page = page;
216                         InitializeAsUserControlInternal ();
217                 }
218
219                 internal void InitializeAsUserControlInternal ()
220                 {
221                         if (initialized)
222                                 return;
223                         initialized = true;
224                         WireupAutomaticEvents ();
225                         FrameworkInitialize ();
226                 }
227
228                 public string MapPath (string virtualPath)
229                 {
230                         return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
231                 }
232
233                 protected override void LoadViewState (object savedState)
234                 {
235                         if (savedState != null) {
236                                 Pair p = (Pair) savedState;
237                                 base.LoadViewState (p.First);
238                                 if (p.Second != null) {
239                                         EnsureAttributes ();
240                                         attrBag.LoadViewState (p.Second);
241                                 }
242                         }
243
244                 }
245
246 #if NET_2_0
247                 protected internal
248 #else
249                 protected
250 #endif
251                 override void OnInit (EventArgs e)
252                 {
253                         InitializeAsUserControl (Page);
254
255                         base.OnInit(e);
256                 }
257
258                 protected override object SaveViewState ()
259                 {
260                         object baseState = base.SaveViewState();
261                         object attrState = null;
262                         if (attributes != null)
263                                 attrState = attrBag.SaveViewState ();
264                         if (baseState == null && attrState == null)
265                                 return null;
266                         return new Pair (baseState, attrState);
267                 }
268
269                 string IAttributeAccessor.GetAttribute (string name)
270                 {
271                         if (attributes == null)
272                                 return null;
273                         return attributes [name];
274                 }
275                 
276                 void IAttributeAccessor.SetAttribute (string name, string value)
277                 {
278                         EnsureAttributes ();
279                         Attributes [name] = value;
280                 }
281
282                 string IUserControlDesignerAccessor.InnerText
283                 {
284                         get {
285                                 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
286                                 if (innerText == null)
287                                         return string.Empty; 
288                                 return innerText;
289                         }
290                         set { ViewState["!DesignTimeInnerText"] = value; }
291                 }
292
293                 string IUserControlDesignerAccessor.TagName
294                 {
295                         get {
296                                 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
297                                 if (innerTag == null)
298                                         return string.Empty; 
299                                 return innerTag;
300                         }
301                         set { ViewState["!DesignTimeTagName"] = value; }
302                 }
303 #if NET_2_0
304                 [MonoTODO ("Not implemented")]
305                 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
306                 {
307                         throw new NotImplementedException ();
308                 }
309
310                 [MonoTODO ("Not implemented")]
311                 bool IFilterResolutionService.EvaluateFilter (string filterName)
312                 {
313                         throw new NotImplementedException ();
314                 }
315 #endif
316         }
317 }