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