Add this for backwards compatibility
[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 #if NET_2_0
49         [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
50         [ParseChildren (true, "", ChildControlType = typeof (Control))]
51 #else
52         [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
53         [ParseChildren (true)]
54 #endif
55         public class UserControl : TemplateControl, IAttributeAccessor, IUserControlDesignerAccessor
56 #if NET_2_0
57         , INamingContainer, IFilterResolutionService
58 #endif
59         {
60                 private bool initialized;
61                 private AttributeCollection attributes;
62                 private StateBag attrBag;
63
64                 public UserControl ()
65                 {
66                 }
67
68                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
69                 [Browsable (false)]
70                 public HttpApplicationState Application
71                 {
72                         get {
73                                 Page p = Page;
74                                 if (p == null)
75                                         return null;
76                                 return p.Application;
77                         }
78                 }
79
80                 private void EnsureAttributes ()
81                 {
82                         if (attributes == null) {
83                                 attrBag = new StateBag (true);
84                                 if (IsTrackingViewState)
85                                         attrBag.TrackViewState ();
86                                 attributes = new AttributeCollection (attrBag);
87                         }
88                 }
89
90                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91                 [Browsable (false)]
92                 public AttributeCollection Attributes
93                 {
94                         get {
95                                 EnsureAttributes ();
96                                 return attributes;
97                         }
98                 }
99
100                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101                 [Browsable (false)]
102                 public Cache Cache
103                 {
104                         get {
105                                 Page p = Page;
106                                 if (p == null)
107                                         return null;
108                                 return p.Cache;
109                         }
110                 }
111
112 #if NET_2_0
113                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
114                 [Browsable (false)]
115                 public ControlCachePolicy CachePolicy 
116                 {
117                         get {
118                                 throw new NotImplementedException ();
119                         }
120                 }
121 #endif          
122
123                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
124                 [Browsable (false)]
125                 public bool IsPostBack
126                 {
127                         get {
128                                 Page p = Page;
129                                 if (p == null)
130                                         return false;
131                                 return p.IsPostBack;
132                         }
133                 }
134
135                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
136                 [Browsable (false)]
137                 public HttpRequest Request
138                 {
139                         get {
140                                 Page p = Page;
141                                 if (p == null)
142                                         return null;
143                                 return p.Request;
144                         }
145                 }
146
147                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
148                 [Browsable (false)]
149                 public HttpResponse Response
150                 {
151                         get {
152                                 Page p = Page;
153                                 if (p == null)
154                                         return null;
155                                 return p.Response;
156                         }
157                 }
158
159                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
160                 [Browsable (false)]
161                 public HttpServerUtility Server
162                 {
163                         get {
164                                 Page p = Page;
165                                 if (p == null)
166                                         return null;
167                                 return p.Server;
168                         }
169                 }
170
171                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
172                 [Browsable (false)]
173                 public HttpSessionState Session
174                 {
175                         get {
176                                 Page p = Page;
177                                 if (p == null)
178                                         return null;
179                                 return p.Session;
180                         }
181                 }
182
183                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
184                 [Browsable (false)]
185                 public TraceContext Trace
186                 {
187                         get {
188                                 Page p = Page;
189                                 if (p == null)
190                                         return null;
191                                 return p.Trace;
192                         }
193                 }
194
195                 [EditorBrowsable (EditorBrowsableState.Never)]
196                 public void DesignerInitialize ()
197                 {
198                         InitRecursive (null);
199                 }
200
201                 [EditorBrowsable (EditorBrowsableState.Never)]
202                 public void InitializeAsUserControl (Page page)
203                 {
204                         if (initialized)
205                                 return;
206                         initialized = true;
207                         this.Page = page;
208                         WireupAutomaticEvents ();
209                         FrameworkInitialize ();
210                 }
211
212                 public string MapPath (string virtualPath)
213                 {
214                         return Request.MapPath (virtualPath, TemplateSourceDirectory, true);
215                 }
216
217                 protected override void LoadViewState (object savedState)
218                 {
219                         if (savedState != null) {
220                                 Pair p = (Pair) savedState;
221                                 base.LoadViewState (p.First);
222                                 if (p.Second != null) {
223                                         EnsureAttributes ();
224                                         attrBag.LoadViewState (p.Second);
225                                 }
226                         }
227
228                 }
229
230 #if NET_2_0
231                 protected internal
232 #else
233                 protected
234 #endif
235                 override void OnInit (EventArgs e)
236                 {
237                         InitializeAsUserControl (Page);
238
239                         base.OnInit(e);
240                 }
241
242                 protected override object SaveViewState ()
243                 {
244                         object baseState = base.SaveViewState();
245                         object attrState = null;
246                         if (attributes != null)
247                                 attrState = attrBag.SaveViewState ();
248                         if (baseState == null && attrState == null)
249                                 return null;
250                         return new Pair (baseState, attrState);
251                 }
252
253                 string IAttributeAccessor.GetAttribute (string name)
254                 {
255                         if (attributes == null)
256                                 return null;
257                         return attributes [name];
258                 }
259                 
260                 void IAttributeAccessor.SetAttribute (string name, string value)
261                 {
262                         EnsureAttributes ();
263                         Attributes [name] = value;
264                 }
265
266                 string IUserControlDesignerAccessor.InnerText
267                 {
268                         get {
269                                 string innerText = ((string) ViewState["!DesignTimeInnerText"]);
270                                 if (innerText == null)
271                                         return string.Empty; 
272                                 return innerText;
273                         }
274                         set { ViewState["!DesignTimeInnerText"] = value; }
275                 }
276
277                 string IUserControlDesignerAccessor.TagName
278                 {
279                         get {
280                                 string innerTag = ((string) ViewState["!DesignTimeTagName"]);
281                                 if (innerTag == null)
282                                         return string.Empty; 
283                                 return innerTag;
284                         }
285                         set { ViewState["!DesignTimeTagName"] = value; }
286                 }
287 #if NET_2_0
288                 [MonoTODO]
289                 int IFilterResolutionService.CompareFilters (string filter1, string filter2)
290                 {
291                         throw new NotImplementedException ();
292                 }
293
294                 [MonoTODO]
295                 bool IFilterResolutionService.EvaluateFilter (string filterName)
296                 {
297                         throw new NotImplementedException ();
298                 }
299 #endif
300         }
301 }